欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

axis2实现webservice分布式开发

程序员文章站 2022-07-13 16:09:22
...

因为工作关系这两天在学习axis2实现webservice,然后就在网上找相关的资料,不过在网上并没有找到我想要的东西,那些资料大多写的很乱且对于初学者难于理解,所以我把我写好的代码贴出来,以后能用的上的朋友可以一起研究下

webservice就是web服务,可实现分布式开发

这里简单介绍下客户端和服务器端得代码,(客户端和服务端可放在一个工程,也可分成两个工程),所需的jar文件如果不知道有哪些就把所有的axis2的jar文件都考进去

1客户端就一个文件

import javax.xml.stream.FactoryConfigurationError;

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.Constants;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;

public class Test {

public static void main(String[] args) {
ServiceClient sender = null;
try {
String uri = "http://localhost:8080/Axis2Server/services/CRMMgrService";
String serverName = "ServerResponse";
// String key = "id";
// String num = "001";
// 创建一OMFactory 对象来创建OMElement对象
OMFactory fac = OMAbstractFactory.getOMFactory();
OMElement method = fac.createOMElement(serverName,
OMAbstractFactory.getOMFactory().createOMNamespace(
"http://mcerp.service.pojo/xsd",
"ServerObjectRequest"));// <axis2ns1:fiEMP
// xmlns:axis2ns1="http://www.tyky.com.cn"
// />
// OMElement value = fac.createOMElement(key, null);// <id />
// value.addChild(fac.createOMText(value, num));
// method.addChild(value);

Options options = new Options();
EndpointReference epr = new EndpointReference(uri);// 獲得代理服務
options.setTo(epr);
options.setAction("urn:" + serverName);// 执行方法

options.setTransportInProtocol(Constants.TRANSPORT_HTTP);// 传输协议为HTTP
options.setProperty(Constants.Configuration.ENABLE_REST,
Constants.VALUE_TRUE);// 方法为rest

sender = new ServiceClient();
sender.setOptions(options);
OMElement result = sender.sendReceive(method);
System.out.println(result.getFirstElement().getText());
} catch (Exception e) {
e.printStackTrace();
} finally {
if (sender != null) {
try {
sender.cleanupTransport();
} catch (AxisFault e) {
e.printStackTrace();
}
}
}
}

}

 
 2.服务端需要一个java文件和services.xml

package com.test.service;
/*
 *   version 1.0
 * 客户关系管理对象容器,包括业务模型有
 * 客户基本资料,客户评估,客户关怀,合同管理等客户业务模型对象
 */



public class CRMMgrService {

	public void ServerObjectRequest(String str)
	{
		System.out.println("hello world!");
	}
	
	public String ServerResponse()
	{
		return "hello-response";
	}
	
	public String ServerPOJOResponse()
	{
		return "hello-pojo";
	}
}

 

3.在服务端的WEB-INF下新建services\CRMMgr\META-INF文件目录,在META-INF目录下新建services.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<service name="CRMMgrService" scope="application">
	<description>the pojo web service interfaces</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>
	<schema schemaNamespace="http://mcerp.service.pojo/xsd" />
	<parameter name="ServiceClass">
		com.test.service.CRMMgrService
	</parameter>
</service>

 这样只要运行客户端代码就能够得到服务端的响应