Axis2 WebService的发布和调用说明
准备工作 1、下载:axis2-1.5.4-bin.zip,axis2-1.5.4-war.zip 下载地址:http://axis.apache.org/axis2/java/core/ 点击Service会进入Service列表页面,当前只有一个Version服务。http://localhost:8085/axis2/services/Version?wsdl 安装完插件后,IDE中选择new->other中看到“Axis2 Wizards”,说明插件已经安装成功了。 ? AXIS2发布Web Services? 1、新建名称为Axis2Service1 的java工程。 package ws; public class TestWs { public String showName(String name) {return name; } public String getName() {return "Axis2Service Sample"; } }
二、arr部署方式 <service name="AxisService"> <description>AxisService</description> <parameter name="ServiceClass">ws.TestWs</parameter> <operation name="showName"> <messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" /> </operation> <operation name="getName"> <messageReceiver class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" /> </operation> </service>
生成aar包 Axis2Service1deploy>jar cvf AxisService.aar . (注意带.号) 2、插件打包 output File location,输入:D: ; output File Name,输入artiver文件的名称 AxisService。点击finish。
打开http://localhost:8085/axis2/services/listServices 就可以看到刚才发布的AxisService服务了,下面有两个函数:showName,getName。 ? 三、独立部署 1、新建java web project工程。
<?xml version="1.0" encoding="UTF-8"?> <web-app id="wmf" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <servlet> <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> </web-app>
2、新建 Axis2Service2srcwsTestWs.java package ws; public class TestWs { public String showName(String name) {return name; } public String getName() {return "Axis2Service Sample"; } }
<service name="AxisService"> <description>AxisService</description> <parameter name="ServiceClass">ws.TestWs</parameter> <operation name="showName"> <messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" /> </operation> <operation name="getName"> <messageReceiver class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" /> </operation> </service>
启动tomcat后,访问http://localhost:8085/Axis2Service2/services/AxisService?wsdl看是否正常。 ?AXIS2调用Web Services 一、客户端stub文件生成 2、插件生成方式 指定输入路径,点击Next 提示:All operations completed successfully! 生成成功。在D:srcws 自动生成了stub一系列文件,其中ws是包名。 上面2种方式生成的stub类有点不一样,脚本生成方式是单一文件,插件生成方式生成的一系列文件。 二、客户端调用 1、新建 java工程 Axis2Client
? //初始化Sub类 AxisServiceStub stub = new AxisServiceStub(); //传递AxisServiceStub.ShowName对象,相关参数在这边赋值。 AxisServiceStub.ShowName command = new AxisServiceStub.ShowName(); command.setName("Hello!"); //取得返回值 String name = stub.showName(command).get_return(); System.out.println(name);
? 调用成功后控制台输出:Hello! ? ?AXIS2调用REST Web Services 使用http://localhost:8086/Axis2Rest/services/AxisService/showName?name=rest的方式访问刚才发布成功的WebService。从上面可以看出这个就是rest风格。Axis1.0是无法通过showName?name=rest来获取信息的。 2、使用axis客户端调用 public class TestRest { private static String toEpr = "http://localhost:8086/Axis2Rest/services/AxisService"; public static void main(String[] args) throws AxisFault { Options options = new Options(); options.setTo(new EndpointReference(toEpr)); //客户端REST方式调用服务跟普通服务的区别,REST调用必须加上下面这个代码。 options.setProperty(Constants.Configuration.ENABLE_REST,Constants.VALUE_TRUE); ServiceClient sender = new ServiceClient(); //axis2-1.5.4不需要下面这句代码,否则会报错 //sender.engageModule(new QName(Constants.MODULE_ADDRESSING)); sender.setOptions(options); OMElement result = sender.sendReceive(getPayload()); try { XMLStreamWriter writer = XMLOutputFactory.newInstance() .createXMLStreamWriter(System.out); result.serialize(writer); writer.flush(); } catch (XMLStreamException e) { e.printStackTrace(); } catch (FactoryConfigurationError e) { e.printStackTrace(); } } private static OMElement getPayload() { OMFactory fac = OMAbstractFactory.getOMFactory(); OMNamespace omNs = fac.createOMNamespace( "http://ws","example1"); OMElement method = fac.createOMElement("showName",omNs); OMElement value = fac.createOMElement("name",omNs); value.addChild(fac.createOMText(value,"Rest")); method.addChild(value); return method; }
说明: 2、客户端REST方式调用服务跟普通服务的区别,就是Rest有下面这个代码; <ns:showNameResponse xmlns:ns="Resthttp://ws"><ns:return>Rest</ns:return></ns:showNameResponse>
3、getPayload方法 OMNamespace omNs = fac.createOMNamespace("http://ws","example1"); 指定命名空间,如果没对的话会报如下错误<faultstring>namespace mismatch require http://ws found http://ws1</faultstring> OMElement method = fac.createOMElement("showName",omNs); 要传递的方法名为 "showName" OMElement value = fac.createOMElement("name",omNs); 传递的参数为name value.addChild(fac.createOMText(value,"Rest"));? 传递参数name的值为Rest。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |