基于AXIS的Web Service在Eclipse的实战应用[转贴]

“鲤鱼网”版权所有,转载请注明出处,谢谢!


1、涉及到的资源的:
Axis压缩包,可以到www.apache.org/dist/ws/下载。版本1.3。
eclipse压缩包,可以到www.eclipse.org下载。版本3.1。
MyEclipse安装文件,可以到http://www.myeclipseide.com下载。版本4.1。
resin服务器,可以到www.caucho.com下载。版本2.1.4。


2、配置环境:
将Axis包解压缩到c:/axis。
配置环境变量批处理文件WSDL2Java.bat。用于从WSDL(Web Service Description Language)文件生成JAVA文件。稍后将用到。


//WSDL2Java.bat源文件
@echo off
set AXIS_HOME=c:/axis
set AXIS_LIB=%AXIS_HOME%/lib
set AXISCLASSPATH=%AXIS_LIB%/axis.jar;%AXIS_LIB%/commons-discovery-0.2.jar;%AXIS_LIB%/commons-logging-1.0.4.jar;%AXIS_LIB%/jaxrpc.jar;%AXIS_LIB%/saaj.jar;%AXIS_LIB%/log4j-1.2.8.jar;%AXIS_LIB%/wsdl4j-1.5.1.jar;%AXIS_LIB%/axis-schema.jar;%AXIS_LIB%/axis-ant.jar
java -cp %AXISCLASSPATH% org.apache.axis.wsdl.WSDL2Java WSDemo.wsdl


解压缩eclipse,并安装MyEclipse。(安装过程略)


解压缩Resin,并在MyEclipse配置Resin。(配置过程略)


在Eclipse创建新工程:webservice,会自动生成src(JAVA源文件)及WebRoot(JSP文件及WEB-INF)两个目录。


将工程加入到Resin中。(加入过程略)


将c:/axis/webapps/axis/目录中文件复制到WebRoot目录下。将工程刷新后,eclipse会将WebRoot下的内容复制到resin/webapps/目录。


访问http://localhost:8080/webservice/。并自动在应用的WEB-INFO目录下生成resin/webapps/webservice/WEB-INF/server-config.wsdd文件。


3、开发应用接口:
在src目录中新建包com.liyunet.ws,在此包中创建类WSDemo.java。


//WSDemo.java源文件
package com.liyunet.ws;


public class WSDemo {


public String hello(String s){
s = "hello," + s;
System.out.println(s);
return s;
}

public String hi(String s){
s = "hi," + s;
System.out.println(s);
return s;
}
}


将WSDemo.java文件复制到WebRoot目录下,并改名为WSDemo.jws。注意必须将包的声明去掉,其它保持不变。


//WSDemo.jws源文件
public class WSDemo {


public String hello(String s){
s = "hello," + s;
System.out.println(s);
return s;
}

public String hi(String s){
s = "hi," + s;
System.out.println(s);
return s;
}
}


访问http://localhost:8080/webservice/WSDemo.jws?WSDL。在网页中会显示WSDL内容。
//页面内容
<?xml version="1.0" encoding="UTF-8" ?>
- <wsdl:definitions targetNamespace="http://localhost:8080/webservice/services/WSDemo" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:intf="http://localhost:8080/webservice/services/WSDemo" xmlns:impl="http://localhost:8080/webservice/services/WSDemo">
- <!--
WSDL created by Apache Axis version: 1.2.1
Built on Jun 14, 2005 (09:15:57 EDT)


-->
- <wsdl:message name="helloResponse">
<wsdl:part name="helloReturn" type="xsd:string" />
</wsdl:message>
- <wsdl:message name="hiResponse">
<wsdl:part name="hiReturn" type="xsd:string" />
</wsdl:message>
- <wsdl:message name="helloRequest">
<wsdl:part name="s" type="xsd:string" />
</wsdl:message>
- <wsdl:message name="hiRequest">
<wsdl:part name="s" type="xsd:string" />
</wsdl:message>
- <wsdl:portType name="WSDemo">
- <wsdl:operation name="hello" parameterOrder="s">
<wsdl:input name="helloRequest" message="impl:helloRequest" />
<wsdl:output name="helloResponse" message="impl:helloResponse" />
</wsdl:operation>
- <wsdl:operation name="hi" parameterOrder="s">
<wsdl:input name="hiRequest" message="impl:hiRequest" />
<wsdl:output name="hiResponse" message="impl:hiResponse" />
</wsdl:operation>
</wsdl:portType>
- <wsdl:binding name="WSDemoSoapBinding" type="impl:WSDemo">
<wsdlsoap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http" />
- <wsdl:operation name="hello">
<wsdlsoap:operation soapAction="" />
- <wsdl:input name="helloRequest">
<wsdlsoap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://ws.liyunet.com" />
</wsdl:input>
- <wsdl:output name="helloResponse">
<wsdlsoap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://localhost:8080/webservice/services/WSDemo" />
</wsdl:output>
</wsdl:operation>
- <wsdl:operation name="hi">
<wsdlsoap:operation soapAction="" />
- <wsdl:input name="hiRequest">
<wsdlsoap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://ws.liyunet.com" />
</wsdl:input>
- <wsdl:output name="hiResponse">
<wsdlsoap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://localhost:8080/webservice/services/WSDemo" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
- <wsdl:service name="WSDemoService">
- <wsdl:port name="WSDemo" binding="impl:WSDemoSoapBinding">
<wsdlsoap:address location="http://localhost:8080/webservice/services/WSDemo" />
</wsdl:port>
</wsdl:service>
</wsdl:definitions>


将网页中显示的XML复制到文件中,并保存为WSDemo.wsdl。(注意将第一行“<?xml version="1.0" encoding="UTF-8" ?>”前面的空格及每行的‘-’去掉)。
//整理后格式
<?xml version="1.0" encoding="UTF-8" ?>
<wsdl:definitions targetNamespace="http://localhost:8080/webservice/services/WSDemo" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:intf="http://localhost:8080/webservice/services/WSDemo" xmlns:impl="http://localhost:8080/webservice/services/WSDemo">
<!--
WSDL created by Apache Axis version: 1.2.1
Built on Jun 14, 2005 (09:15:57 EDT)


-->
<wsdl:message name="helloResponse">
<wsdl:part name="helloReturn" type="xsd:string" />
</wsdl:message>
<wsdl:message name="hiResponse">
<wsdl:part name="hiReturn" type="xsd:string" />
</wsdl:message>
<wsdl:message name="helloRequest">
<wsdl:part name="s" type="xsd:string" />
</wsdl:message>
<wsdl:message name="hiRequest">
<wsdl:part name="s" type="xsd:string" />
</wsdl:message>
<wsdl:portType name="WSDemo">
<wsdl:operation name="hello" parameterOrder="s">
<wsdl:input name="helloRequest" message="impl:helloRequest" />
<wsdl:output name="helloResponse" message="impl:helloResponse" />
</wsdl:operation>
<wsdl:operation name="hi" parameterOrder="s">
<wsdl:input name="hiRequest" message="impl:hiRequest" />
<wsdl:output name="hiResponse" message="impl:hiResponse" />
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="WSDemoSoapBinding" type="impl:WSDemo">
<wsdlsoap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http" />
<wsdl:operation name="hello">
<wsdlsoap:operation soapAction="" />
<wsdl:input name="helloRequest">
<wsdlsoap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://ws.liyunet.com" />
</wsdl:input>
<wsdl:output name="helloResponse">
<wsdlsoap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://localhost:8080/webservice/services/WSDemo" />
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="hi">
<wsdlsoap:operation soapAction="" />
<wsdl:input name="hiRequest">
<wsdlsoap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://ws.liyunet.com" />
</wsdl:input>
<wsdl:output name="hiResponse">
<wsdlsoap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://localhost:8080/webservice/services/WSDemo" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="WSDemoService">
<wsdl:port name="WSDemo" binding="impl:WSDemoSoapBinding">
<wsdlsoap:address location="http://localhost:8080/webservice/services/WSDemo" />
</wsdl:port>
</wsdl:service>
</wsdl:definitions>


并把WSDemo.wsdl及WSDL2Java.bat两个文件复制到src目录下,执行WSDL2Java.bat文件,它会自动生成调用WebService接口的程序代码。


利用Eclipse重构,将自动生成的包名改为com.liyunet.ws.wsdemo。


修改应用下的server-config.wsdd文件,添加如下内容:
//添加的内容
<service name="WSDemo" provider="java:RPC">
<parameter name="className" value="com.liyunet.ws.WSDemo"/>
<parameter name="allowedMethods" value="*"/>
<parameter name="scope" value="Request"/>
</service>


将WSDemo添加到AXIS的服务中。


然后我们新建JUnit的TestCase来测试一下调用Web Service接口。


首先新建WSFactory.java,用来取得服务接口的实例。


//WSFactory.java源文件
package com.liyunet.ws;


import java.net.MalformedURLException;
import java.net.URL;


import javax.xml.rpc.ServiceException;


import com.liyunet.ws.wsdemo.WSDemoServiceLocator;


public class WSFactory {


private static com.liyunet.ws.wsdemo.WSDemo demo = null;


public static com.liyunet.ws.wsdemo.WSDemo getWSDemo() {
if (demo == null) {
String url = "http://localhost:8080/webservice/services/WSDemo";
try {
demo = (new WSDemoServiceLocator().getWSDemo(new URL(url)));
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ServiceException e) {
e.printStackTrace();
}
}
return demo;
}
}


在测试文件中调用服务接口。


//WSDemoTest.java源文件
package test;


import java.rmi.RemoteException;


import junit.framework.TestCase;


import com.liyunet.ws.WSFactory;


public class WSTest extends TestCase {


protected void setUp() throws Exception {
super.setUp();
}


protected void tearDown() throws Exception {
super.tearDown();
}

public void testWSDemo()
{
com.liyunet.ws.wsdemo.WSDemo demo = WSFactory.getWSDemo();
try {
demo.hi("Axis");
} catch (RemoteException e) {
e.printStackTrace();
}
}


}


可以在Resin的控制台中看到打印出的“hi,Axis”。


有的朋友会问,如果我再想添加新的接口该怎么做呢?我觉得重新再自动生成一次比较好。


过程:修改WSDemo.java -> 复制到WSDemo.jws -> 生成WSDemo.wsdl -> 生成调用接口类并修改包名 -> JUnit测试


以上是AXIS的最简单的应用,可以说是一个入门的参考。希望对想学习AXIS的初学者有所帮助。

你可能感兴趣的:(eclipse,Web,应用服务器,webservice,SOAP)