Java Axis2 webservice

Posted Mr. Li

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java Axis2 webservice相关的知识,希望对你有一定的参考价值。

 

 

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 testAxis2 {

	public static void main(String[] args) {

		String url = "http://192.168.1.1/xxx/DTIService?wsdl";
		String namespace = "http://xxx.com.cn";
		String methodName = "processMessage";
		testDocument(url, namespace, methodName);

	}

	/**
	 * 方法二: 应用document方式调用 用ducument方式应用现对繁琐而灵活。现在用的比较多。因为真正摆脱了我们不想要的耦合
	 */
	public static void testDocument(String url, String namespace,
			String methodName) {
		try {

			String xmlData = "<ROOTMAIN><CASEMAIN><SUBTYPECODE>10101</SUBTYPECODE><EVENTGRADECODE>1</EVENTGRADECODE><EVENTID>2875715</EVENTID><DAMAGEGRADENAME>暂无</DAMAGEGRADENAME><EVENTDESC>北四环西路辅路与六郞庄路交叉口向北50米六郞庄路东侧有绿地脏乱</EVENTDESC><EVENTTYPECODE>1</EVENTTYPECODE><PARTCODE></PARTCODE><ADDRESS>六郎庄37号杆(东南20米)</ADDRESS><OPERATORNAME></OPERATORNAME><TASKNUM>16110803978</TASKNUM><CELLCODE>11010802300219</CELLCODE><TELNUM>13910351520</TELNUM><INSTDATE>2016-11-08T14:24:12</INSTDATE><STREETCODE>110108023</STREETCODE><CREATEDATE>2016-11-08T14:24:12</CREATEDATE><RECDISPNUM>2016海淀管字1793973</RECDISPNUM><MAINTYPECODE>1</MAINTYPECODE><EVENTSRCCODE>1</EVENTSRCCODE><COMMUNITYCODE>110108023002</COMMUNITYCODE><DISTRICTCODE>110108</DISTRICTCODE><PATROLNAME>杜文森</PATROLNAME><COORDINATEY>312853.0</COORDINATEY><COORDINATEX>494444.0</COORDINATEX></CASEMAIN><CASEOPERATIONDETAIL><SIGNHUMANNAME>冯文越</SIGNHUMANNAME><SIGNDATE>2016-11-11T08:51:13</SIGNDATE></CASEOPERATIONDETAIL></ROOTMAIN>";

			Options options = new Options();
			// 指定调用WebService的URL
			EndpointReference targetEPR = new EndpointReference(url);
			options.setTo(targetEPR);
			// options.setAction("urn:getPrice");

			ServiceClient sender = new ServiceClient();
			sender.setOptions(options);

			OMFactory fac = OMAbstractFactory.getOMFactory();
			// 命名空间,有时命名空间不增加没事,不过最好加上,因为有时有事,你懂的
			OMNamespace omNs = fac.createOMNamespace(namespace, methodName);

			/* 以下设置添加SOAP消息头 */
			OMElement SenderCode = fac.createOMElement("SenderCode", omNs);
			OMElement ReceiverCode = fac.createOMElement("ReceiverCode", omNs);
			OMElement OperationCode = fac
					.createOMElement("OperationCode", omNs);
			OMElement MessageID = fac.createOMElement("MessageID", omNs);
			SenderCode.setText("002");
			ReceiverCode.setText("000");
			OperationCode.setText("011");
			MessageID.setText("10000000000000000");

			sender.addHeader(SenderCode);
			sender.addHeader(ReceiverCode);
			sender.addHeader(OperationCode);
			sender.addHeader(MessageID);

			/* 以下设置要发送的参数 */
			// 指定方法 名
			OMElement method = fac.createOMElement(methodName, omNs);
			// 指定方法的参数名
			OMElement symbol = fac.createOMElement("MessageInfo", omNs);
			symbol.setText(xmlData);
			// symbol.addChild(fac.createOMText(symbol, xmlData));
			method.addChild(symbol);
			method.build();

			OMElement res = sender.sendReceive(method);

			String result = res.getFirstElement().getText();
			System.out.println(result);

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

}

  

以上是关于Java Axis2 webservice的主要内容,如果未能解决你的问题,请参考以下文章

java如何通过eclipse安装axis2来调用webservice

java怎么使用axis调用webservice

Java Axis2 webservice

如何在eclipse创建基于axis2的webservice服务端

如何解决java axis 调用webservice服务器操作异常

Java调用wsdl,怎么实现