Axis2 WebService Client

1、Axis2调用WebService一般有两种方法:
a、使用org.apache.axis2.rpc.client.RPCServiceClient自动生成PayLoad
b、使用org.apache.axis2.client.ServiceClient手动生成PayLoad

package com.ats.ws.client;

import javax.xml.namespace.QName;

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;
import org.apache.axis2.rpc.client.RPCServiceClient;
import org.apache.log4j.Logger;

import com.ats.db.Person;
import com.ats.db.PersonAddResponse;

public class WSInvoker
{
	private static Logger logger = Logger.getLogger(WSInvoker.class);
	
	private static void invokeWebServiceRPC()
	{
		try
		{
			RPCServiceClient serviceClient = new RPCServiceClient();
			Options options = serviceClient.getOptions();
			options.setTo(new EndpointReference("http://localhost:8080/WebService1/services/PersonManager"));

			// 调用Action
			QName operation = new QName("http://ws.ats.com", "PersonAdd");

			// 调用参数类型和参数值
			Person p = new Person();
			Object req[] = new Object[] { p };
			p.setName("HI");
			Class[] reqClasses = new Class[] { PersonAddResponse.class };

			// 返回结果
			Object[] rsp = serviceClient.invokeBlocking(operation, req, reqClasses);
			PersonAddResponse r = (PersonAddResponse) rsp[0];
			logger.debug(r.getOperationReult());

		} catch (AxisFault e)
		{
			e.printStackTrace();
		}
	}

	public static void invokeWebServiceDocument()
	{
		try
		{
			ServiceClient serviceClient = new ServiceClient();
			Options opts = new Options();
			opts.setTo(new EndpointReference("http://localhost:8080/WebService1/services/PersonManager"));
			opts.setAction("urn:PersonAdd");
			serviceClient.setOptions(opts);

			// 发送请求并并得到返回结果
			OMElement res = serviceClient.sendReceive(createPayLoad());
			logger.debug(res.getFirstElement().getText());
		} catch (AxisFault e)
		{
			e.printStackTrace();
		}
	}

	public static OMElement createPayLoad()
	{
		OMFactory fac = OMAbstractFactory.getOMFactory();
		// 命名空间
		OMNamespace omNsXSD = fac.createOMNamespace("http://ws.ats.com", "xsd");
		OMNamespace omNsXS = fac.createOMNamespace("http://www.w3.org/2001/XMLSchema", "xs");
		// 指定方法
		OMElement method = fac.createOMElement("PersonAdd", omNsXSD);
		// 指定方法的参数
		OMElement p1 = fac.createOMElement("person", omNsXS);
		
		OMElement personName = fac.createOMElement("personName", omNsXS);
		personName.setText("personName");
		p1.addChild(personName);
		OMElement personid=fac.createOMElement("personid", omNsXS);
		personid.setText("");
		p1.addChild(personid);
		
		method.addChild(p1);
		return method;
	}

	public static void main(String[] args)
	{
		WSInvoker.invokeWebServiceRPC();
		WSInvoker.invokeWebServiceDocument();
	}
}

2、要想看到网络通讯的话,在log4j的配置文件中增加:

	<category name="org.apache.commons.httpclient">
		<priority value="WARN" />
	</category>
	<category name="httpclient.wire">
		<priority value="DEBUG" />
	</category>

Leave a Reply

Your email address will not be published. Required fields are marked *

*