Axis2是由大家熟悉的Axis1.x系列演化过来,对它进行了彻底的重写,它使用了新的模块化架构,更方便于功能性的扩展等等。
Axis2支持WS-Addressing、WS-RM、WS-Security和WS-I BasicProfile,WS-Policy将在新版本里得到支持。
Axis2支持更多的data bindings,包括XMLBeans、JiBX、JaxMe和JaxBRI,以及它原生的data binding(ADB)。
一、范例测试环境:
axis2-1.5.1
jdk1.5.0_15
apache-tomcat-5.5.23
二、部署步骤
1、将Axis2的二进制工程axis2-1.5.1-bin.zip解压到C:\axis2-1.5.1。设置环境变量AXIS2_HOME=C:\axis2-1.5.1,在Path环境变量追加“%AXIS2_HOME%\bin”串。
2、新增一个web工程(工程名为axis2),将Axis2自带的axis2.war范例中WEB-INF下的conf、lib、modules、services文件夹拷贝到新工程的WEB-INF下。
3、在web.xml文件中增加以下配置信息:
- <servlet>
- <servlet-name>Axis2Servlet</servlet-name>
- <servlet-class>org.apache.axis2.transport.http.AxisServlet</servlet-class>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>Axis2Servlet</servlet-name>
- <url-pattern>*.jws</url-pattern>
- </servlet-mapping>
- <servlet-mapping>
- <servlet-name>Axis2Servlet</servlet-name>
- <url-pattern>/services/*</url-pattern>
- </servlet-mapping>
4、增加一个用于作为WebService发布的Java类,源码如下:
- package com.achievo.ws;
- import com.achievo.bean.UserBean;
- public class SimService {
- public String sayHello(String name){
- return "Hello " + name;
- }
- public Double getPrice(){
- return 123.45;
- }
- public String[] getArray(){
- String[] arr = new String[2];
- arr[0] = "11111";
- arr[1] = "22222";
- return arr;
- }
- public UserBean getUser(){
- UserBean user = new UserBean("cjm", "123");
- return user;
- }
- }
- package com.achievo.bean;
- public class UserBean {
- private String username;
- private String password;
- public UserBean(){
- }
- public UserBean(String uid, String pwd){
- this.username = uid;
- this.password = pwd;
- }
- public String getUsername() {
- return username;
- }
- public void setUsername(String username) {
- this.username = username;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- }
5、在WEB-INF\services\目录下新增一个文件夹sim(名称随意),在sim文件夹中增加一个子文件夹META-INF,在META-INF中增加一个名为services.xml的文件,该xml文件的信息如下:
- <serviceGroup>
- <service name="SimService" scope="application">
- <description>Sim Service</description>
- <messageReceivers>
- <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only" class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver"/>
- <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out" class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>
- </messageReceivers>
- <parameter name="ServiceClass">com.achievo.ws.SimService</parameter>
- </service>
- </serviceGroup>
表示将com.achievo.ws.SimService类以SimService名称发布为WebService。
6、将新工程部署到Tomcat,并启动Tomcat。在浏览器的地址中输入以下URL:
http://localhost:8888/axis2/services/SimService?wsdl,如果正常显示wsdl定义信息,则表示部署成功。
7、新工程目录结构
三、应用范例
1、在DOS中定位到C:\axis2-1.5.1\bin目录,然后执行以下指令:
wsdl2java -uri http://localhost:8888/axis2/services/SimService?wsdl -p com.achiveo.ws -s -o stub
-url参数:指定了wsdl文件的路径
-p参数:指定了生成的Java类的包名
-o参数:指定了生成的一系列文件保存的根目录
2、将生成的SimServiceStub.java文件引用到测试工程中。
3、测试类代码如下:
- public class Test {
- public static void main(String[] args) {
- try{
- SimServiceStub stub = new SimServiceStub();
- //方法返回字符串值
- SayHello sayHello = new SimServiceStub.SayHello();
- sayHello.setName("cjm");
- SimServiceStub.SayHelloResponse res = stub.sayHello(sayHello);
- String result = res.get_return();
- System.out.println(result);
- //方法返回double值
- System.out.println(stub.getPrice().get_return());
- //方法返回数组
- String[] arr = stub.getArray().get_return();
- if(arr!=null && arr.length>0){
- for(int i=0;i<arr.length;i++){
- System.out.println(arr[i]);
- }
- }
- //方法返回bean对象
- SimServiceStub.UserBean user = stub.getUser().get_return();
- System.out.println(user.getUsername() + ", " + user.getPassword());
- }catch(Exception ex){
- ex.printStackTrace();
- }
- }
- }
- MonicaServicesImplServiceStub stub = new MonicaServicesImplServiceStub();
- MonicaServicesImplServiceStub.CreatePnr createPnr = new MonicaServicesImplServiceStub.CreatePnr();
- createPnr.setArg0(companyCode);
- createPnr.setArg1(workNo);
- createPnr.setArg2(officeNo);
- createPnr.setArg3(pid);
- createPnr.setArg4(passengerIds);
- createPnr.setArg5(airCodes);
- createPnr.setArg6(isDomestic);
- CreatePnrE createPnrE = new MonicaServicesImplServiceStub.CreatePnrE();
- createPnrE.setCreatePnr(createPnr);
- CreatePnrResponseE createPnrResponseE = stub.createPnr(createPnrE);
- String result = createPnrResponseE.getCreatePnrResponse().get_return();
- System.out.println(result);
如果是通过代理访问公网,那么在调用webservice方法之前需要设置代理属性:
- ProxyProperties proxyProperties = new ProxyProperties();
- proxyProperties.setProxyName("xxx.xxx.xxx.xxx");
- proxyProperties.setProxyPort(80);
- options.setProperty(HTTPConstants.PROXY, proxyProperties);
- options.setProperty(HTTPConstants.HTTP_PROTOCOL_VERSION, HTTPConstants.HEADER_PROTOCOL_10);
- options.setTimeOutInMilliSeconds(10000);
也可以将class类打包成aar文件,然后发布成WebService
1、将class类和services.xml文件拷贝到一个新的目录中,目录结构如下图:
class类与包目录结构一致,services.xml文件放到META-INF文件夹中。
2、 在DOS中定位到../aar/sim目录中,然后执行以下命令:
jar cvf SimService.aar . (注意最后那个点哦),将会在../aar/sim目录中生成SimService.aar文件。
3、将SimService.aar文件拷贝到WEB-INF\services\目录下,此时,services目录的结构应该为:
四、将Axis2整合到spring环境
1、在web.xml文件中增加以下配置信息:
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>WEB-INF/applicationContext.xml</param-value>
- </context-param>
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
2、将要作为WebService发布的java类配置为一个bean:
- <bean id="simService" class="com.achievo.ws.SimService"/>
3、将services.xml文件的内容调整为以下信息:
- <serviceGroup>
- <service name="SimService" scope="application">
- <description>SimService</description>
- <parameter name="ServiceObjectSupplier">
- org.apache.axis2.extensions.spring.receivers.SpringServletContextObjectSupplier
- </parameter>
- <parameter name="SpringBeanName">simService</parameter>
- <messageReceivers>
- <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only" class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver"/>
- <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out" class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>
- </messageReceivers>
- </service>
- </serviceGroup>