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

客户端编程:

import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;

public class TestClient {
	public static void main(String[] args) {
		try {
			ServiceClient sc = new ServiceClient();
			Options opts = sc.getOptions();
			opts.setTo(new EndpointReference("http://localhost:8080/axis2/services/MyService"));
			opts.setAction("urn:sayHello");
			OMElement res = sc.sendReceive(createPayLoad());
			System.out.println(res);
		} catch (AxisFault e) {
			e.printStackTrace();
		}

	}

	public static OMElement createPayLoad() {
		OMFactory fac = OMAbstractFactory.getOMFactory();
		OMNamespace omNs = fac.createOMNamespace("http://ws.apache.org/axis2","nsl");
		OMElement method = fac.createOMElement("sayHello", omNs);
		OMElement value = fac.createOMElement("name", omNs);
		value.setText("admin");
		method.addChild(value);
		return method;
	}

}

 

我们再讲一下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>

你可能感兴趣的:(java,apache,spring,webservice,配置管理)