java webservice与axis2

由于最近做的一个项目需要与SAP通过WebServices进行接口传输数据。所以花时间去研究了一下当前的两种WebServices框架(axis2和XFire)。期间还是碰上了不少的异常, 现在就一起来分享一下我的心得吧!

主要的步骤如下:

1:下载axis2-1.4.1-bin.zip和axis2-1.4.1-war.zip两个支持包(由于附件大小限制,大家就自己到http://ws.apache.org/axis2/download/1_4_1/download.cgi自行去下载)

2:将两个包解压,将axis2.war放到服务器的发布目录下。

3: 访问一下http://localhost:8080/axis2就可以对webservices进行管理了。登录的用户名和密码默认(admin/axis2) 可以在/WEB-INF/conf/axis2.xml 中进行配置.

4: 用eclipse建一个工程。建立一个普通的java类如下:

 

package com.wujianjun.axis2;

public class MyServices {

 public String sayHello(String name) {
  return "你好:"+ name;
 }

}

5:在src目录下建一个/src/META-INF/services.xml. 里面进行如下配置

<?xml version="1.0" encoding="UTF-8"?>
<serviceGroup>
 <service name="MyService">
  <description>Service description</description>
  <parameter name="ServiceClass">
   com.wujianjun.axis2.MyServices 
     </parameter>
  <messageReceivers>
   <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"
    class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
   <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only"
    class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" />
  </messageReceivers>
 </service>
</serviceGroup>

5: 将这个类利用eclipse的功能将src选中后导出成一个jar文件(service.jar)。

6: 访问http://localhost:8080/axis2进入到管理页面。再选择upload services, 再把services.jar 改成services.aar 后上传到服务器上去。再进入services页面则可看到刚发布的web services了。

7: 调用web services : 访问http://localhost:8080/axis2/services/MyService/。我们就可以看到服务器返回给我们的"你好: admin";sayHello?name=admin

客户端编程:

Java代码 
  1. import org.apache.axiom.om.OMAbstractFactory;  
  2. import org.apache.axiom.om.OMElement;  
  3. import org.apache.axiom.om.OMFactory;  
  4. import org.apache.axiom.om.OMNamespace;  
  5. import org.apache.axis2.AxisFault;  
  6. import org.apache.axis2.addressing.EndpointReference;  
  7. import org.apache.axis2.client.Options;  
  8. import org.apache.axis2.client.ServiceClient;  
  9.   
  10. public class TestClient {  
  11.     public static void main(String[] args) {  
  12.         try {  
  13.             ServiceClient sc = new ServiceClient();  
  14.             Options opts = sc.getOptions();  
  15.             opts.setTo(new EndpointReference("http://localhost:8080/axis2/services/MyService"));  
  16.             opts.setAction("urn:sayHello");  
  17.             OMElement res = sc.sendReceive(createPayLoad());  
  18.             System.out.println(res);  
  19.         } catch (AxisFault e) {  
  20.             e.printStackTrace();  
  21.         }  
  22.   
  23.     }  
  24.   
  25.     public static OMElement createPayLoad() {  
  26.         OMFactory fac = OMAbstractFactory.getOMFactory();  
  27.         OMNamespace omNs = fac.createOMNamespace("http://ws.apache.org/axis2","nsl");  
  28.         OMElement method = fac.createOMElement("sayHello", omNs);  
  29.         OMElement value = fac.createOMElement("name", omNs);  
  30.         value.setText("admin");  
  31.         method.addChild(value);  
  32.         return method;  
  33.     }  
  34.   
  35. }  

 

我们再讲一下services.xml配置方式:

1、第一种配置方式: 按方法名称进行配置
<operation name="methodName">
 <!--表示有参有返回值的方法-->
 <messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
 <!--表示有参没有返回值的方法-->
 <messageReceiver class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" />
</operation>

2、第二种配置方式:
<messageReceivers>
 <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out" class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
 <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only" class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" />
</messageReceivers>

3、普通类配置:
<service name="serviceName">
 <Description>服务的描述</Description>
 <parameter name="ServiceClass">全类名</parameter>
 <messageReceivers>
  <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"
   class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
  <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only"
   class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" />
 </messageReceivers>
</service>

4、与Spring集成配置(web工程):
<service name="MySpringService" targetNamespace="http://www.wujianjun.org">
 <Description>测试spring与axis集成(web工程)</Description>
 <parameter name="ServiceObjectSupplier">org.apache.axis2.extensions.spring.receivers.SpringServletContextObjectSupplier</parameter>
 <parameter name="SpringBeanName">springBeanName</parameter>
 <messageReceivers>
  <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"
   class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
  <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only"
   class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" />
 </messageReceivers>
</service>

5、与Spring集成配置(JAVA工程):
<service name="MySpringService" targetNamespace="http://www.wujianjun.org">
 <Description>测试spring与axis集成(web工程)</Description>
 <parameter name="ServiceObjectSupplier">org.apache.axis2.extensions.spring.receivers.SpringAppContextAwareObjectSupplier</parameter>
 <parameter name="SpringBeanName">springBeanName</parameter>
 <messageReceivers>
  <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"
   class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
  <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only"
   class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" />
 </messageReceivers>
</service>

 

axis2与spring集成有两种方式一种就是针对web工程进行集成,另一种则是针对JAVA工程集成。下面我们就一个一个来集成吧。
一、axis2与Spring集成(Web程序中)
1: 把axis2放入到服务器的发布目录下,把spring.jar放入到/axis2/WEB-INF/lib下.
2: 把axis2中lib下的包引入工程之中(把spring.jar放入到/axis2/WEB-INF/lib下)
 axiom-api-1.2.7.jar
 axiom-impl-1.2.7.jar
 axis2-adb-1.4.1.jar
 axis2-kernel-1.4.1.jar
 axis2-spring-1.4.1.jar
 backport-util-concurrent-3.1.jar
 commons-codec-1.3.jar
 commons-httpclient-3.1.jar
 commons-logging-1.1.1.jar
 neethi-2.0.4.jar
 spring.jar
 woden-api-1.0M8.jar
 woden-impl-dom-1.0M8.jar
 wsdl4j-1.6.2.jar
 XmlSchema-1.4.2.jar
 annogen-0.1.0.jar
3: 在web.xml中加入
 <!-- spring -->
    <listener>
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
     <param-name>contextConfigLocation</param-name>
     <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
 <!-- webServices -->
 <servlet>
        <servlet-name>AxisServlet</servlet-name>
        <display-name>Apache-Axis Servlet</display-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>/servlet/AxisServlet</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>AxisServlet</servlet-name>
        <url-pattern>*.jws</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>AxisServlet</servlet-name>
        <url-pattern>/services/*</url-pattern>
    </servlet-mapping>
4: 编写service类和对应的方法并在applicationContext.xml中进行Bean的配置
5: 在WEB-INF下建services/模板名称/META-INF/services.xml
6: 然后再services.xml中对服务进行配置就可以了,访问:http://localhost:8080/projName/services/serviceName?wsdl就可以访问到对应的服务描述文件了。

二、axis2与Spring集成(JAVA程序中)
1: 把axis2放入到服务器的发布目录下,把spring.jar放入到/axis2/WEB-INF/lib下.
2: 把axis2中lib下的包引入工程之中
 axiom-api-1.2.7.jar
 axiom-impl-1.2.7.jar
 axis2-adb-1.4.1.jar
 axis2-kernel-1.4.1.jar
 axis2-spring-1.4.1.jar
 backport-util-concurrent-3.1.jar
 commons-codec-1.3.jar
 commons-httpclient-3.1.jar
 commons-logging-1.1.1.jar
 neethi-2.0.4.jar
 spring.jar
 woden-api-1.0M8.jar
 woden-impl-dom-1.0M8.jar
 wsdl4j-1.6.2.jar
 XmlSchema-1.4.2.jar
 annogen-0.1.0.jar
3: 编写service类和对应的方法并在applicationContext.xml中进行Bean的配置
4:在applicationContext.xml中引入axis2的bean
 <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd" >
 <beans>
  <bean id="applicationContext" class="org.apache.axis2.extensions.spring.receivers.ApplicationContextHolder"  />
 </beans>
5: 建立一个初始化spring容器的类(com.wujianjun.axis2.java.SpringInit)实现ServiceLifeCycle接口。编写startUp方法如下:
 ClassLoader classLoader = service.getClassLoader();
    ClassPathXmlApplicationContext appCtx = new ClassPathXmlApplicationContext(new String[] {"applicationContext.xml"}, false);
    appCtx.setClassLoader(classLoader);
    appCtx.refresh();
6: 在src下建meta-inf/services.xml,内容如下:
 <?xml version="1.0" encoding="UTF-8"?>
 <serviceGroup>
  <service name="MySpringAxis2" class="com.wujianjun.axis2.java.SpringInit">
   <description>Spring aware</description>
   <parameter name="ServiceObjectSupplier">
    org.apache.axis2.extensions.spring.receivers.SpringAppContextAwareObjectSupplier
   </parameter>
   <parameter name="SpringBeanName">
    springService
      </parameter>
   <messageReceivers>
    <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"
     class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
    <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only"
     class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" />
   </messageReceivers>
  </service>
 </serviceGroup>
7: 把src下的文件把成jar包后,改名为*.aar 放入/axis2/WEB-INF/services下。
8: 启动服务器,访问http://localhost:8080/projName/services/serviceName?wsdl就可以访问到对应的服务描述文件了。

对应的services.xml配置。我们在第一讲里写的请清楚了。具体的请参考第一讲的内容。 

你可能感兴趣的:(webservice)