axis2操作webservice
程序员文章站
2022-05-09 08:45:33
...
最近要与别的公司做接口,用到了axis2,所以就在网上找些资料了解下,自己做了个小例子,发布与调用webservice
1.下载axis2
地址:http://axis.apache.org/axis2/java/core/download.cgi
下载Binary Distribution,axis2-1.6.2-bin.zip中包含了axis2所有jar,把jar包加入项目中
2.创建需要发布的服务
3.编写web.xml及services.xml
将下载的axis2-1.6.2-bin.zip中webapp/web-inf/web.xml复制到项目中,去掉不需要的配置
services.xml
如果只发布单个服务,则用<service></service>
如果发布多个服务,则用<serviceGroup><service></service></serviceGroup>
这里要注意下services.xml的放置位置
4.运行发布服务
测试地址:http://127.0.0.1:8080/axisforWebService/services/HelloWorld?wsdl
打开如下图则说明发布成功了
好了,服务现在已经发布了,下面我们来调用这个服务
5.调用服务
方式一:
运行结果:Current passengers: <ns:saySorryResponse xmlns:ns="http://service.dm.com"><ns:return>sorry, heiwado.</ns:return></ns:saySorryResponse>
方式二:
方式三:
好了,服务调用就先说到这了,下次再去弄下cxf
技术交流群:312740759
1.下载axis2
地址:http://axis.apache.org/axis2/java/core/download.cgi
下载Binary Distribution,axis2-1.6.2-bin.zip中包含了axis2所有jar,把jar包加入项目中
2.创建需要发布的服务
package com.dm.service; public class HelloWorld { public String sayHello(String name) { return "Hello, " + name + "."; } public String saySorry(String name) { return "sorry, " + name + "."; } public String getWorld() { return "Hello, world!"; } }
3.编写web.xml及services.xml
将下载的axis2-1.6.2-bin.zip中webapp/web-inf/web.xml复制到项目中,去掉不需要的配置
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> <web-app> <display-name>Apache-Axis2</display-name> <servlet> <servlet-name>AxisServlet</servlet-name> <display-name>Apache-Axis Servlet</display-name> <servlet-class>org.apache.axis2.transport.http.AxisServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet> <servlet-name>AxisAdminServlet</servlet-name> <display-name>Apache-Axis AxisAdmin Servlet (Web Admin)</display-name> <servlet-class> org.apache.axis2.webapp.AxisAdminServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>AxisServlet</servlet-name> <url-pattern>/servlet/AxisServlet</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>AxisServlet</servlet-name> <url-pattern>*.jws</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>AxisServlet</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>AxisAdminServlet</servlet-name> <url-pattern>/axis2-admin/*</url-pattern> </servlet-mapping> <mime-mapping> <extension>inc</extension> <mime-type>text/plain</mime-type> </mime-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> <welcome-file>index.html</welcome-file> <welcome-file>/axis2-web/index.jsp</welcome-file> </welcome-file-list> </web-app>
services.xml
如果只发布单个服务,则用<service></service>
如果发布多个服务,则用<serviceGroup><service></service></serviceGroup>
<serviceGroup> <service name="HelloWorld"> <description> HelloWorld Service Example </description> <parameter name="ServiceClass"> com.dm.service.HelloWorld </parameter> <!-- 当不指定operation时必须加上这个 <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> --> <operation name="sayHello"> <messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" /> </operation> <operation name="saySorry"> <messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" /> <!-- 有返回值 --> </operation> <operation name="getWorld"> <messageReceiver class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" /> <!-- 无返回值 --> </operation> </service> <!-- <service name="weather"> <description> getWeather </description> <parameter name="ServiceClass"> com.dm.service.impl.WeatherImpl </parameter> <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> <operation name="getWeather"> <messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" /> </operation> </service> --> </serviceGroup>
这里要注意下services.xml的放置位置
4.运行发布服务
测试地址:http://127.0.0.1:8080/axisforWebService/services/HelloWorld?wsdl
打开如下图则说明发布成功了
好了,服务现在已经发布了,下面我们来调用这个服务
5.调用服务
方式一:
package common; 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.Constants; import org.apache.axis2.addressing.EndpointReference; import org.apache.axis2.client.Options; import org.apache.axis2.client.ServiceClient; public class TestWebservice { private static EndpointReference targetEPR = new EndpointReference( "http://localhost:8080/axisforWebService/services/HelloWorld"); public static OMElement sayHello(String symbol) { OMFactory fac = OMAbstractFactory.getOMFactory(); OMNamespace omNs = fac .createOMNamespace("http://service.dm.com", "tns"); OMElement method = fac.createOMElement("saySorry", omNs); OMElement value = fac.createOMElement("name", omNs); value.addChild(fac.createOMText(value, symbol)); method.addChild(value); return method; } public static void main(String[] args) { try { OMElement getPassenger = sayHello("heiwado"); Options options = new Options(); options.setTo(targetEPR); options.setTransportInProtocol(Constants.TRANSPORT_HTTP); ServiceClient sender = new ServiceClient(); sender.setOptions(options); OMElement result = sender.sendReceive(getPassenger); String response = result.getFirstElement().getText(); System.err.println("Current passengers: " + result); } catch (Exception e) { e.printStackTrace(); } } }
运行结果:Current passengers: <ns:saySorryResponse xmlns:ns="http://service.dm.com"><ns:return>sorry, heiwado.</ns:return></ns:saySorryResponse>
方式二:
package common; 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.addressing.EndpointReference; import org.apache.axis2.client.Options; import org.apache.axis2.client.ServiceClient; public class WebserviceUtils { public static void main(String[] args) { try { ServiceClient sc = new ServiceClient(); Options opts = new Options(); opts.setTo(new EndpointReference( "http://localhost:8080/axisforWebService/services/HelloWorld")); opts.setAction("urn:echo"); opts.setTimeOutInMilliSeconds(10000); sc.setOptions(opts); OMElement res = sc.sendReceive(createPayLoad()); System.out.println(res); } catch (AxisFault e) { e.printStackTrace(); } } public static OMElement createPayLoad() { OMFactory fac = OMAbstractFactory.getOMFactory(); OMNamespace omNs = fac.createOMNamespace( "http://service.dm.com", "HelloWorld"); OMElement method = fac.createOMElement("sayHello", omNs); OMElement value = fac.createOMElement("name", omNs); value.setText("hehaisu"); method.addChild(value); return method; } }
方式三:
package common; import javax.xml.namespace.QName; import org.apache.axis2.addressing.EndpointReference; import org.apache.axis2.client.Options; import org.apache.axis2.rpc.client.RPCServiceClient; public class WebserviceUtils4 { public static void main(String[] args) { String url = "http://localhost:8080/axisforWebService/services/HelloWorld"; String result = null; try { // 使用RPC方式调用WebService RPCServiceClient serviceClient = new RPCServiceClient(); Options options = serviceClient.getOptions(); // 指定调用WebService的URL EndpointReference targetEPR = new EndpointReference(url); options.setTo(targetEPR); // 在创建QName对象时,QName类的构造方法的第一个参数表示WSDL文件的命名空间名,也就是<wsdl:definitions>元素的targetNamespace属性值 // // 指定要调用的getWorld方法及WSDL文件的命名空间..... QName opAddEntry = new QName("http://service.dm.com", "saySorry"); // // 指定getGreeting方法的参数值,如果有多个,继续往后面增加即可,不用指定参数的名称 Object[] opAddEntryArgs = new Object[] { "上海" }; // 返回参数类型,这个和axis1有点区别 // invokeBlocking方法有三个参数,其中第一个参数的类型是QName对象,表示要调用的方法名; // 第二个参数表示要调用的WebService方法的参数值,参数类型为Object[]; // 第三个参数表示WebService方法的返回值类型的Class对象,参数类型为Class[]。 // 当方法没有参数时,invokeBlocking方法的第二个参数值不能是null,而要使用new Object[]{} // 如果被调用的WebService方法没有返回值,应使用RPCServiceClient类的invokeRobust方法, // 该方法只有两个参数,它们的含义与invokeBlocking方法的前两个参数的含义相同 // 指定getGreeting方法返回值的数据类型的Class对象..... Class[] classes = new Class[] { String.class }; // 调用getGreeting方法并输出该方法的返回值....... result = (String)serviceClient.invokeBlocking(opAddEntry, opAddEntryArgs, classes)[0]; //调用无返回值的方法则用serviceClient.invokeRobust方法 //serviceClient.invokeRobust(opAddEntry, opAddEntryArgs); System.out.println(result); } catch (Exception e) { e.printStackTrace(); } } }
好了,服务调用就先说到这了,下次再去弄下cxf
技术交流群:312740759