zhao5243
深爱甜蜜
2018-08-09 08:35
采纳率: 50%
浏览 7.8k
已采纳

webservice调用时怎么传参数

<?xml version="1.0" encoding="UTF-8"?>

-

-

-xsd:schema

/xsd:schema

-

-

-

-

-

-

-

-

-

-

下面是我的调用代码:

 Service service = new Service();
        Call call = (Call) service.createCall();
        String url = "http://192.1.1.64:8082/Service/ServiceHello?wsdl";
        call.setTargetEndpointAddress(url);
        call.setOperationName(new QName("http://webservice_302/", "getValue"));
        call.addParameter(new QName("http://webservice_302/", "name"), org.apache.axis.encoding.XMLType.XSD_STRING,
                javax.xml.rpc.ParameterMode.IN);
        call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);
        String result = (String) call.invoke(new String[] { "11" });
        System.out.println(result);

下面是接口的方法点:

 public String getValue(String name){
        return "欢迎你! "+name;
    }

可以成功调用,返回结果是:欢迎你! null

说明参数没有传进去,请问这里我应该怎么写呢?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

5条回答 默认 最新

  • xcgh
    xcgh 2018-08-09 09:10
    已采纳

    入参类型应该是org.apache.axis.Constants.XSD_STRING,不认为是org.apache.axis.encoding.XMLType.XSD_STRING

    点赞 评论
  • zhao5243
    深爱甜蜜 2018-08-09 08:39
    
    <?xml version="1.0" encoding="UTF-8"?><!-- Published by JAX-WS RI (http://jax-ws.java.net). RI's version is JAX-WS RI 2.2.9-b130926.1035 svn-revision#5f6196f2b90e9460065a4c2f4e30e065b245e51e. --><!-- Generated by JAX-WS RI (http://jax-ws.java.net). RI's version is JAX-WS RI 2.2.9-b130926.1035 svn-revision#5f6196f2b90e9460065a4c2f4e30e065b245e51e. --><definitions xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://webservice_302/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://webservice_302/" name="HelloService">
    <types>
    <xsd:schema>
    <xsd:import namespace="http://webservice_302/" schemaLocation="http://192.1.1.64:8082/Service/ServiceHello?xsd=1"></xsd:import>
    </xsd:schema>
    </types>
    <message name="getValue">
    <part name="parameters" element="tns:getValue"></part>
    </message>
    <message name="getValueResponse">
    <part name="parameters" element="tns:getValueResponse"></part>
    </message>
    <portType name="Hello">
    <operation name="getValue">
    <input wsam:Action="http://webservice_302/Hello/getValueRequest" message="tns:getValue"></input>
    <output wsam:Action="http://webservice_302/Hello/getValueResponse" message="tns:getValueResponse"></output>
    </operation>
    </portType>
    <binding name="HelloPortBinding" type="tns:Hello">
    <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"></soap:binding>
    <operation name="getValue">
    <soap:operation soapAction=""></soap:operation>
    <input>
    <soap:body use="literal"></soap:body>
    </input>
    <output>
    <soap:body use="literal"></soap:body>
    </output>
    </operation>
    </binding>
    <service name="HelloService">
    <port name="HelloPort" binding="tns:HelloPortBinding">
    <soap:address location="http://192.1.1.64:8082/Service/ServiceHello"></soap:address>
    </port>
    </service>
    </definitions>
    
    点赞 评论
  • qq_35280588
    qq_35280588 2018-08-09 08:54

    public static String rpcCall(String serverName, Object[] parameters) throws AxisFault {
    RPCServiceClient serviceClient = new RPCServiceClient();
    EndpointReference targetEPR = new EndpointReference(Constant.WEB_SERVICE_WSDL_URL);
    Options options = serviceClient.getOptions();
    options.setManageSession(true);
    options.setProperty(HTTPConstants.REUSE_HTTP_CLIENT,true);
    options.setTo(targetEPR);
    options.setAction("urn:"+serverName);
    QName qname = new QName(Constant.NAME_SPACE, serverName);
    OMElement element = serviceClient.invokeBlocking(qname, parameters);
    String result = element.getFirstElement().getText();
    serviceClient.cleanupTransport();
    return result;
    }

    点赞 评论
  • qq_35280588
    qq_35280588 2018-08-09 08:56

    /**
    * 服务调用
    * @param serverName 服务名称
    * @param parameters 业务参数(接口中规定的方法的参数)
    * @return
    * @throws AxisFault
    */
    public static String rpcCall(String serverName, Object[] parameters) throws AxisFault {
    //使用rpc方式调用webService
    RPCServiceClient serviceClient = new RPCServiceClient();
    //调用指定webService的url
    EndpointReference targetEPR = new EndpointReference(Constant.WEB_SERVICE_WSDL_URL);
    Options options = serviceClient.getOptions();
    //客户端开启session对话管理
    options.setManageSession(true);
    options.setProperty(HTTPConstants.REUSE_HTTP_CLIENT,true);
    //确定目标服务地址
    options.setTo(targetEPR);
    //确定调用方法
    options.setAction("urn:"+serverName);
    //指定接口的命名空间和调用的方法名
    QName qname = new QName(Constant.NAME_SPACE, serverName);
    //调用方法传递参数 调用服务 获取服务返回的结果集
    OMElement element = serviceClient.invokeBlocking(qname, parameters);
    //返回结果就是由一段OMElement对象封装的xml字符串
    //我们可以对之灵活应用,下面我取第一个元素值,并打印之,因为调用的方法返回一个结果
    String result = element.getFirstElement().getText();
    serviceClient.cleanupTransport();
    return result;
    }

    点赞 评论
  • qq_34346915
    卡哇伊程序员 2018-08-09 09:09

    https://download.csdn.net/download/qq_34346915/10593706 里面有全套的接口生成以及调用

    点赞 评论

相关推荐