一种是打aar包放在指定位置(适合发布),一种是不打aar包(适合调试)。
?
说明:Eclipse版本luna、tomcat版本6.0、AXIS2版本1.6.2,Eclipse_TomcatPluginV3.3.1
?
??一、Axis2的下载和安装
???? 1.可从http://ws.apache.org/axis2/ 下载Axis2的最新版本:
????? 可以下载如下两个zip包:
????? axis2-1.6.2-bin.zip
????? axis2-1.6.2-war.zip
????? 其中 axis2-1.6.2-bin.zip文件中包含了Axis2中所有的jar文件,
????? axis2-1.6.2-war.zip文件用于将WebService发布到Web容器中。
???? 2.将axis2-1.6.2-war.zip文件解压到相应的目录,将目录中的axis2.war文件放到<Tomcat安装目录>webapps目录中,
???? 并启动Tomcat,在浏览器地址栏中输入如下的URL:
???? http://localhost:8080/axis2/,如看到axis2的主页面则安装成功。
??二、编写和发布WebService
在Eclipse下建立Java Project,工程名HelloFriend。
建包com.dm.service,包下建类HelloFriend。代码如下:
?
Java代码
在工程下新文件夹services/HelloFriend,HelloFriend下按类HelloFriend.java包路径建文件路径com/dm/service,
最后将编译后的HelloFriend.class拷入com/dm/service下。
HelloFriend文件夹下在新建文件夹META-INF,META-INF下新建services.xml。
最后在Windows命令行下进到services/HelloWord目录下运行:
jar cvf HelloFriend.aar . (最后有个点号),
会在HelloFriend目录下生成HelloFriend.aar包。
services.xml内容如下:
??
?
把HelloFriend.aar拷入tomcat安装目录下的webapps/axis2/WEB-INF/services下,再进入http://localhost:8080/axis2/进入Services就会看到HelloFriend了。
?
二.不打包发布:
在Eclipse下新建Tomcat Project,工程名:HelloWorld。新建包com.cm.service,新建类HelloWorld,代码如下:
?
在WEB-INF目录下新建web.xml文件,内容如下:
[xhtml]
view plain
copy
print
?
- <?xml?version="1.0"?encoding="UTF-8"?>??
- <web-app?id="wmf"?version="2.4"?xmlns="http://java.sun.com/xml/ns/j2ee"??
- ????xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"??
- ????xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee?http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">??
- ????<servlet>??
- ????????<servlet-name>AxisServlet</servlet-name>??
- ????????<servlet-class>org.apache.axis2.transport.http.AxisServlet</servlet-class>??
- ????????<load-on-startup>1</load-on-startup>??
- ????</servlet>??
- ????<servlet-mapping>??
- ????????<servlet-name>AxisServlet</servlet-name>??
- ????????<url-pattern>/services/*</url-pattern>??
- ????</servlet-mapping>??
- </web-app>??
把tomcat安装目录下的webapps/axis2/WEB-INF下的modules、service和conf文件件拷至HelloWorld下的WEB-INF目录下。
把lib下的如下jar包夜拷过去,以下jar包已经是最精简的了。

?
?
然后再services下新建HelloWorld/META-INF路径,META-INF下新建services.xml,内容如下:
[xhtml]
view plain
copy
print
?
- <service?name="HelloWorld">??
- ????<description>??
- ????????HelloWorld?Service?Example??
- ????</description>??
- ????<parameter?name="ServiceClass">??
- ????????com.dm.service.HelloWorld????
- ????</parameter>??
- ????<operation?name="sayHello">??
- ????????<messageReceiver?class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"?/>??
- ????</operation>??
- ????<operation?name="saySorry">??
- ????????<messageReceiver?class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"?/>??
- ????</operation>??
- ????<operation?name="getWorld">??
- ????????<messageReceiver?class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver"?/>??
- ????</operation>??
- </service>??
?
启动tomcat后访问http://localhost:8080/HelloWorld/services/HelloWorld?wsdl能看到服务信息了。
?
说明:
1. 第一种方法不用打aar包,把整个文件夹拷过去也是可以的。
2. 第二种方式只是相当于是在AXIS2工程上再开发。(得引入AXIS2的包,web.xml里要加AxisServlet)。同样需要services.xml。
?
??三、客户端调用WebService:
调用WebService的客户端代码如下:
- import?javax.xml.namespace.QName;??
- import?org.apache.axis2.AxisFault;??
- import?org.apache.axis2.addressing.EndpointReference;??
- import?org.apache.axis2.client.Options;??
- import?org.apache.axis2.rpc.client.RPCServiceClient;??
- public?class?TestMain?{??
- public?static?void?main(String?args[])?throws?AxisFault{??
- ?????
- ????RPCServiceClient?serviceClient?=?new?RPCServiceClient();??
- ????Options?options?=?serviceClient.getOptions();??
- ??????
- ????EndpointReference?targetEPR?=?new?EndpointReference(??
- ????????????http://localhost:8080/axis2/services/HelloFriend);??
- ????options.setTo(targetEPR);??
- ??????
- ????Object[]?opAddEntryArgs?=?new?Object[]?{"美女"};??
- ??????
- ????Class[]?classes?=?new?Class[]?{String.class};??
- ??????
- ????QName?opAddEntry?=?new?QName("http://service.dm.com",?"sayHello");??
- ??????
- ????System.out.println(serviceClient.invokeBlocking(opAddEntry,?opAddEntryArgs,?classes)[0]);??
- }??
- }??
import javax.xml.namespace.QName;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;
public class TestMain {
public static void main(String args[]) throws AxisFault{
// 使用RPC方式调用WebService
RPCServiceClient serviceClient = new RPCServiceClient();
Options options = serviceClient.getOptions();
// 指定调用WebService的URL
EndpointReference targetEPR = new EndpointReference(
"http://localhost:8080/axis2/services/HelloService");
options.setTo(targetEPR);
// 指定sayHelloToPerson方法的参数值
Object[] opAddEntryArgs = new Object[] {"美女"};
// 指定sayHelloToPerson方法返回值的数据类型的Class对象
Class[] classes = new Class[] {String.class};
// 指定要调用的sayHelloToPerson方法及WSDL文件的命名空间
QName opAddEntry = new QName("http://ws.apache.org/axis2","sayHelloToPerson");
// 调用sayHelloToPerson方法并输出该方法的返回值
System.out.println(serviceClient.invokeBlocking(opAddEntry,opAddEntryArgs,classes)[0]);
}
}
?
???输出结果为:
? ?hello,美女
??在编写客户端代码时应注意如下几点:
???? 1. 客户端代码需要引用很多Axis2的jar包,如果读者不太清楚要引用哪个jar包,
??????? 可以在Eclipse的工程中引用Axis2发行包的lib目录中的所有jar包。
???? 2. 在本例中使用了RPCServiceClient类的invokeBlocking方法调用了WebService中的方法。
?????? invokeBlocking方法有三个参数,其中第一个参数的类型是QName对象,表示要调用的方法名;
?????? 第二个参数表示要调用的WebService方法的参数值,参数类型为Object[];
?????? 第三个参数表示WebService方法的返回值类型的Class对象,参数类型为Class[]。
?????? 当方法没有参数时,invokeBlocking方法的第二个参数值不能是null,而要使用new Object[]{}。
???? 3. 如果被调用的WebService方法没有返回值,应使用RPCServiceClient类的invokeRobust方法,
??????? 该方法只有两个参数,它们的含义与invokeBlocking方法的前两个参数的含义相同。
???? 4. 在创建QName对象时,QName类的构造方法的第一个参数表示WSDL文件的命名空间名,
???? ?也就是<wsdl:definitions>元素的targetNamespace属性值。
?
??四、用wsdl2java简化客户端的编写
??Axis2提供了一个wsdl2java.bat命令可以根据WSDL文件自动产生调用WebService的代码。
??wsdl2java.bat命令可以在<Axis2安装目录>/bin目录中找到。
??在使用wsdl2java.bat命令之前需要设置AXIS2_HOME环境变量,该变量值是<Axis2安装目录>。
??在Windows控制台输出如下的命令行来生成调用WebService的代码:
??%AXIS2_HOME%binwsdl2java -uri http://localhost:8080/axis2/services/HelloFriend?wsdl
?????????-p client -s -o stub
??其中-url参数指定了wsdl文件的路径,可以是本地路径,也可以是网络路径。
??-p参数指定了生成的Java类的包名,-o参数指定了生成的一系列文件保存的根目录。
??在执行完上面的命令后,就会发现在当前目录下多了个stub目录,
??在stub/src/client目录可以找到一个HelloServiceStub.java文件,
??该文件复杂调用WebService,可以在程序中直接使用这个类,代码如下:
- package?client;??
- public?class?StupTest?{???????????
- ????public?static?void?main(String[]?args)?throws?Exception????
- ????{??
- ????????HelloServiceStub?stub?=?new?HelloServiceStub();??
- ????????HelloServiceStub.SayHelloToPerson?gg?=?new?HelloServiceStub.sayHello();??
- ????????gg.setName("美女");??
- ????????System.out.println(?stub.sayHello().get_return());??
- ????????System.out.println(stub.sayHelloToPerson(gg).get_return());??
- ????}???
- }??
package client;
public class StupTest {
public static void main(String[] args) throws Exception
{
HelloServiceStub stub = new HelloServiceStub();
HelloServiceStub.SayHelloToPerson gg = new HelloServiceStub.SayHelloToPerson();
gg.setName("美女");
System.out.println( stub.sayHello().get_return());
System.out.println(stub.sayHelloToPerson(gg).get_return());
}
}
?
???输出结果如下:
??hello
??hello,美女
上面的代码大大简化了调用WebService的步骤,并使代码更加简洁。 ??但要注意的是,wsdl2java.bat命令生成的Stub类将WebService方法的参数都封装在了相应的类中, ??类名为方法名,例如,sayHelloToPerson方法的参数都封装在了SayHelloToPerson类中, ??要想调用sayHelloToPerson方法,必须先创建SayHelloToPerson类的对象实例。?