Axis2 create webservice implement
发布时间:2020-12-17 00:18:14 所属栏目:安全 来源:网络整理
导读:前言:Web Service是现在最适合实现SOA的技术,而Axis2是实现Web Service的一种技术框架(架构)。 Axis2是下一代 Apache Axis。Axis2 虽然由 Axis 1.x 处理程序模型提供支持,但它具有更强的灵活性并可扩展到新的体系结构。 Axis2 基于新的体系结构进行了全
前言:Web Service是现在最适合实现SOA的技术,而Axis2是实现Web Service的一种技术框架(架构)。
Axis2是下一代 Apache Axis。Axis2 虽然由 Axis 1.x 处理程序模型提供支持,但它具有更强的灵活性并可扩展到新的体系结构。
Axis2 基于新的体系结构进行了全新编写,而且没有采用 Axis 1.x 的常用代码。支持开发 Axis2 的动力是探寻模块化更强、
灵活性更高和更有效的体系结构,这种体系结构可以很容易地插入到其他相关 Web 服务标准和协议(如 WS-Security、
WS-ReliableMessaging 等)的实现中。Apache Axis2 是Axis的后续版本,是新一代的SOAP引擎。
Axis official web site : http://axis.apache.org/ Axis2 Java: http://axis.apache.org/axis2/java/core/ -截止:2013-05-30 12:57:00 ?最新的Axis2版本为1.6.2,我使用的是1.6.0 1、下载axis2. ? 注:解压下载,找到bin目录下的axis2server.bat,双击启动axis2. ---准备需要用到的jar包: activation-1.1.jar axiom-api-1.2.11.jar axiom-dom-1.2.11.jar axiom-impl-1.2.11.jar axis.jar axis2-adb-1.6.0.jar axis2-json-1.6.0.jar axis2-kernel-1.6.0.jar commons-codec-1.3.jar commons-httpclient-3.1.jar commons-logging-1.1.1.jar jaxrpc.jar wsdl4j-1.6.2.jar wstx-asl-3.2.9.jar XmlSchema-1.4.7.jar2、编写Java Bean ---创建一个工程名为mywebsercice,并编写Java类。 package com.boonya.webservice; public class HelloWorld { public String sayHello(String name) { return "Hello," + name + "n"; } } 3、编写服务配置 在再src下面建一个META-INF的文件夹,创建一个services.xml的文件,文件内容如下: <?xml version="1.0" encoding="UTF-8"?> <!-- service (HelloWorld service name)--> <service name="HelloWorld"> <Description>helloWorld example description </Description> <!-- class --> <parameter name="ServiceClass" locked="false">com.boonya.webservice.HelloWorld</parameter> <!-- method --> <operation name="sayHello"> <messageReceiver class="org.apache.axis2.rpc.receivers.RPCReceiver"/> </operation> </service> 4、部署webservice服务 把这个工程打包为jar文件,然后把扩展名jar改为aar,放到TomCat目录webappaxis2WEB-INFservices的目录下面,启动tomcat服务。 5、查看服务 在浏览器输入:http://localhost:8080/axis2/services/,如果看到HelloWorld服务表示web服务部署成功 6、编写测试客户端 package com.boonya.webservice; import java.net.MalformedURLException; import java.rmi.RemoteException; import java.util.Iterator; import javax.xml.namespace.QName; import javax.xml.rpc.ServiceException; import org.apache.axiom.om.OMElement; import org.apache.axis.client.Call; import org.apache.axis.client.Service; import org.apache.axis2.AxisFault; import org.apache.axis2.client.Options; import org.apache.axis2.rpc.client.RPCServiceClient; public class HelloWorldClient { public void testMethod() { String method = "sayHello"; Service service = new Service(); Call call = null; try { call = (Call) service.createCall(); try { call.setTargetEndpointAddress(new java.net.URL("http://localhost:8080/axis2/services/HelloWorld")); } catch (MalformedURLException e) { e.printStackTrace(); } call.setOperationName(new QName("http://com/boonya/webservice",method)); call.setUseSOAPAction(true); call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING); call.setSOAPActionURI("http://com/boonya/webservice/GetServerList"); String k = null; try { k = (String) call.invoke(new Object[]{}); System.out.println(">>> " + k); // 返回值输出 } catch (RemoteException e) { e.printStackTrace(); } // 因为返回值是String类型,所以这里调用的返回值也是String类型 } catch (ServiceException e) { e.printStackTrace(); } } @SuppressWarnings( { "rawtypes" }) public void testMethodRpc() throws AxisFault { RPCServiceClient rpcClient = new RPCServiceClient(); Options opt = new Options(null); opt.setTo(new org.apache.axis2.addressing.EndpointReference("http://localhost:8080/axis2/services/HelloWorld")); opt.setAction("urn:sayHello"); // 方法 rpcClient.setOptions(opt); OMElement element = rpcClient.invokeBlocking(new QName("http://com/boonya/webservice","sayHello"),new Object[]{ null }); // null表示没有参数传递 Iterator values = element.getChildrenWithName(new QName("http://com/boonya/webservice","return")); // return表示有返回值 while (values.hasNext()) { // 遍历出获取的数据 OMElement omElement = (OMElement) values.next(); System.out.println(omElement.getText()); } } public static void main(String[] args) throws AxisFault { HelloWorldClient client=new HelloWorldClient(); client.testMethod(); client.testMethodRpc(); } } About resource from:http://www.360doc.com/content/08/0124/17/21290_1001075.shtml (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |