加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

利用axis2调用webservice

发布时间:2020-12-16 22:38:16 所属栏目:安全 来源:网络整理
导读: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.RPCServiceC
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; ? ? ? } ? }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读