package com.zjaisino.service; import javax.xml.namespace.QName; import org.apache.axis2.AxisFault; import org.apache.axis2.addressing.EndpointReference; import org.apache.axis2.client.Options; import org.apache.axis2.rpc.client.RPCServiceClient; public class GetWSByAxis2 { /** * @param args */ public static void main(String[] args) throws Exception { // Todo Auto-generated method stub try { new GetWSByAxis2().test(); } catch(Exception e){ System.out.println(e.getMessage()); } } public void test() throws Exception { // 服务器端WebService的WSDL连接串 String serviceUrl = "http://localhost:8080/WebService/services/SendFpData?wsdl"; RPCServiceClient serviceClient = null; String resultString = ""; serviceClient = getServiceClient(serviceUrl); // 服务器端开放的方法名 String wsFunction = "SendZfchData"; System.out.println(wsFunction); // 要传给服务器开放方法的参数. String jsonString = "你好" ; resultString = login(serviceUrl,serviceClient,jsonString,wsFunction); System.out.println("resultString=" + resultString); } public RPCServiceClient getServiceClient(String wsdlUrl) { RPCServiceClient serviceClient = null; try { serviceClient = new RPCServiceClient(); Options options = serviceClient.getoptions(); EndpointReference targetEPR = new EndpointReference(wsdlUrl); options.setTo(targetEPR); } catch (AxisFault e) { e.printstacktrace(); } return serviceClient; } public String login(String serviceUrl,RPCServiceClient serviceClient,String jsonString,String wsFunction) throws AxisFault { // 在创建QName对象时,QName类的构造方法的第一个参数表示WSDL文件的命名空间名,也就是<wsdl:deFinitions>元素的targetNamespace属性值 QName opLogin = new QName("http://service.zjaisino.com",wsFunction); // 参数,如果有多个,继续往后面增加即可,不用指定参数的名称 Object[] inputArgs = new Object[] { }; if(jsonString!=null&&!"".equals(jsonString)){ inputArgs = new Object[] { jsonString }; } /* 返回参数类型,这个和axis1有点区别 invokeBlocking方法有三个参数,其中第一个参数的类型是QName对象,表示要调用的方法名; 第二个参数表示要调用的WebService方法的参数值,参数类型为Object[]; 第三个参数表示WebService方法的返回值类型的Class对象,参数类型为Class[]。 当方法没有参数时,invokeBlocking方法的第二个参数值不能是null,而要使用new Object[]{} 如果被调用的WebService方法没有返回值,应使用RPCServiceClient类的invokeRobust方法, 该方法只有两个参数,它们的含义与invokeBlocking方法的前两个参数的含义相同 */ Class[] returnTypes = new Class[] { String.class }; Object[] response = serviceClient.invokeBlocking(opLogin,inputArgs,returnTypes); String result = (String) response[0]; // display results if (result == null) { System.out.println("result is null!"); } else { System.out.println(wsFunction+": " + result); } return result; } }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。