利用Aixs2框架,在Eclipse开发平台实现Webservice服务的实现、发布、调用。
【实验环境】:
1、MyEclipse10
2、Tomcat 7..0
3、Axis2
-
下载axis,http://download.csdn.net/detail/u011731233/8743263把下载的war包放到tomcat的webapps目录,启动tomcat服务,在浏览器地址栏输入http://localhost:8080/axis2/验证axis安装正常
-
新建java project,建立下列工程目录
新建web服务文件
package com;
public class Hello {
public String hw(){
return "hello,world";
}
}
创建一个services.xml的文件
<service name="Hello">
<description>
helloworld example description
</description>
<parameter name="ServiceClass" locked="false">
com.Hello
</parameter>
<operation name="hw">
<messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"></messageReceiver>
</operation>
</service>
-
把这个工程打包为jar文件,然后把扩展名jar改为aar,放到TomCat目录\webapp\axis2\WEB-INF\services的目录下面,启动tomcat服务。在地址栏输入:http://localhost:8080/axis2/services/listServices 验证服务配置正常
(选中要打包的工程->右键选择Export---->java----->java file----->next----->Exported all output folders for checked project----->Export destination输入jar包存放的位置以及文件名---->finish)
-
新建客户端调用web服务文件
package com; import java.util.Iterator; 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 Client { public static void main(String[] args) throws Exception { RPCServiceClient rpcClient = new RPCServiceClient(); Options opt = new Options(); opt.setTo(new EndpointReference( "http://localhost:8080/axis2/services/Hello")); opt.setAction("urn:hw"); rpcClient.setOptions(opt); OMElement element = rpcClient.invokeBlocking( new javax.xml.namespace.QName("http://com", "hw"), new Object[] { null }); Iterator values = element .getChildrenWithName(new javax.xml.namespace.QName( "http://com", "return")); while (values.hasNext()) { OMElement omElement = (OMElement) values.next(); System.out.println(omElement.getText()); } } }