Eclipse已有web工程集成 Axis2

来源:互联网 发布:如何修改mac用户名 编辑:程序博客网 时间:2024/05/07 00:28

目的:使用Axis2发布webservice并使用客户端调用.

1.到http://ws.apache.org/axis2/1_4_1/installationguide.html上面下载 axis2-1.4.1-bin.zip ,里面有JKD,Tomcat版本的要求.

2.下载到的axis2-1.4.1-bin.zip解压到axis2-1.4.1-bin目录.Copy lib目录到已有工程目录.

3.web.xml增加servlet配置.

<servlet><description></description><display-name>Apache-Axis Servlet</display-name><servlet-name>AxisServlet</servlet-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>/services/*</url-pattern></servlet-mapping>

4.启动已有问工程,测试是否报错.若有 javax.xml.包找不到的错误,建议使用 jdk1.6.或者找到jar包加载进去.

5.增加一个Class.

package example;public class HelloWorldService {public HelloWorldService() {}public String hello(String userName) {return "Hello axis2 service!" + userName;}public String helloNoParam() {return "测试!";}}

6.手工配置 services.xml.(目录结构为:WEB-INF/services/HW/META-INF/services.xml,其中HW可以命名为其他.文件夹不存在的就新增.)

<serviceGroup>

<service name="HelloWorldService">

<description>Hello World Service</description>

<parameter name="ServiceClass" locked="false">example.HelloWorldService</parameter>

<operation name="hello" mep="http://www.w3.org/2004/08/wsdl/in-out">

<messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />

</operation>

<operation name="helloNoParam">

<messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />

</operation>

</service>

</serviceGroup>

 

7.重新打包并在Tomcat中运行.在IE中输入http://localhost:8080/testProject/services/HelloWorldService?wsdl,可以看到发布的服务,xml.有的浏览器中只会显示服务名,如Google Chrome.

8.使用Java调用刚发布的接口.

首先,使用个通用类(在某论坛里面看到的一个兄弟的通用方法.)

package example;

 

import java.util.Iterator;

 

import javax.xml.namespace.QName;

 

import org.apache.axiom.om.OMElement;

import org.apache.axis2.addressing.EndpointReference;

import org.apache.axis2.client.Options;

import org.apache.axis2.rpc.client.RPCServiceClient;

 

public class Axis2CommonClient {

public Axis2CommonClient() {

 

}

 

/**

* @param srvcUrl

*            访问的url

* @param namespace

*            命名空间

* @param operateName

*            你想调用的方法

* @param param

*            要传入的参数。

* @return

* @throws Exception

*/

public Object excute(String srvcUrl, String namespace, String operateName,

Object param[]) throws Exception {

QName qname = new QName(namespace, operateName);

RPCServiceClient client = new RPCServiceClient();

Options options = new Options();

options.setTo(new EndpointReference(srvcUrl));

options.setAction("urn:" + operateName);

client.setOptions(options);

OMElement element = client.invokeBlocking(qname, param);

if (element != null && !"".equals(element)) {

Iterator<?> values = element.getChildrenWithName(new QName(

namespace, "return"));

while (values.hasNext()) {

OMElement omElement = (OMElement) values.next();

return omElement.getText();

}

} else {

return element;

}

return "-1";

 

}

}

9.客户端代码.

package example;

 

public class TestClient {

public static void main(String[] args) throws Exception {

String url = "http://localhost:8080/testProject/services/HelloWorldService";

String namespace = "http://example";//此处的命名空间 example 应与发布的服务中的命名空间保持一致.

String operateName = "hello";

String[] param = new String[] { "xx" };

Axis2CommonClient axClient = new Axis2CommonClient();

Object obj = axClient.excute(url, namespace, operateName, param);

System.out.println(obj);

operateName = "helloNoParam";

obj = axClient.excute(url, namespace, operateName, param);

System.out.println(obj);

}

}

10.Run AS Java Application,打印出

 

Hello axis2 service!xx

测试!

11.可以运行.原理以及一些细节的配置,可以自行揣摩.

原创粉丝点击