java 调用webservice sopui解析出来方法都带前缀无法正常调用

网上的wsdl 说明示例:

有个项目上遇到解析出来都是带有前缀的,例如:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://services.founder.com">
   <soapenv:Header/>
   <soapenv:Body>
      <ser:funInterFaceMZBYJ>
         <ser:in0>?</ser:in0>
      </ser:funInterFaceMZBYJ>
   </soapenv:Body>
</soapenv:Envelope>
View Code

之前一直用的hutool 调用 webservice,都比较好用,但是遇到这种奇怪的格式就一直报命名空间的错误。只有换种方式,网上搜了下,亲测可用。参考地址:(20条消息) Java调用WebService的几种方式_世界joker的博客-CSDN博客_java webservice

具体步骤如下

1、引用包

<!--spring web Service的包-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web-services</artifactId>
            <version>1.5.2.RELEASE</version>
        </dependency>

        <!--spring web service wsdl包-->
        <dependency>
            <groupId>wsdl4j</groupId>
            <artifactId>wsdl4j</artifactId>
            <version>1.6.3</version>
        </dependency>
        <!-- axis 1.4 jar start -->
        <dependency>
            <groupId>org.apache.axis</groupId>
            <artifactId>axis</artifactId>
            <version>1.4</version>
        </dependency>
        <dependency>
            <groupId>commons-discovery</groupId>
            <artifactId>commons-discovery</artifactId>
            <version>0.2</version>
            <exclusions>
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.axis</groupId>
            <artifactId>axis-jaxrpc</artifactId>
            <version>1.4</version>
        </dependency>
        <dependency>
            <groupId>org.apache.axis</groupId>
            <artifactId>axis-saaj</artifactId>
            <version>1.4</version>
        </dependency>
        <dependency>
            <groupId>wsdl4j</groupId>
            <artifactId>wsdl4j</artifactId>
            <version>1.4</version>
        </dependency>

2、执行示例

 //wsdl地址
        String endpoint ="http://100.100.100.63:8888/founderWebs/services/ICalculateServicePZH";
        //命名空间
        String namespace = "http://www.w3.org/2001/XMLSchema";
        //服务名
        String serviceName = "ICalculateServicePZH";
        //方法名
        String methodName = "funInterFaceMZBYJ";
        //soapAction
        String soapAction = "";


        Service service = new Service();
        Call call = (Call) service.createCall();
        //设置响应超时
        call.setTimeout(3000);

        //设置地址
        call.setTargetEndpointAddress(endpoint);

        //设置方法名
        call.setOperationName(new QName(namespace,methodName));

        String param = "<funderService functionName='MZBYJ_201'>" +
                "              <value>1</value>" +
                "   <value>"+code+"</value>   " +
                "  </funderService>";
        //设置参数
        call.addParameter("Message", XMLType.XSD_STRING, ParameterMode.IN);
        String message = param;
        Object [] obj = {message};

        //设置返回类型
        call.setReturnType(XMLType.XSD_STRING);

        //启用soap
        call.setUseSOAPAction(true);
        //设置soapAction
        call.setSOAPActionURI(soapAction);

        //设置服务名
        SOAPService soapService = new SOAPService();
        soapService.setName(serviceName);
        call.setSOAPService(soapService);
        String ret = (String) call.invoke(obj);
View Code

 3、多参数使用

/**
     * 调用WebService接口之前,方法参数及参数赋值
     * 当前接口中参数类型仅限于字符串、整数、布尔,参数为空的情况为字符串类型
     * @param call 接口访问请求
     * @param keys 字段(方法参数)
     * @param values 字段值(参数对应值)
     *
     */
    private static void params(Call call, Object[] keys, Object[] values) {

        if(keys == null || values == null) return;
        if(keys.length == 0 || values.length == 0) return;
        if(keys.length != values.length){
            throw new IllegalArgumentException("接口方法参数与参数值不匹配!");
        }
        for(int i = 0; i < values.length; i++){

            String key = (String) keys[i]; //方法参数
            Object value = values[i]; //参数值
            if(value == null){
                call.addParameter(key, XMLType.XSD_STRING, ParameterMode.IN);
            } else if(value instanceof String){
                call.addParameter(key, XMLType.XSD_STRING, ParameterMode.IN);
            } else if(value instanceof Integer){
                call.addParameter(key, XMLType.XSD_INTEGER, ParameterMode.IN);
            } else if(value instanceof Boolean){
                call.addParameter(key, XMLType.XSD_BOOLEAN, ParameterMode.IN);
            }

        }

    }
多参数

4、多参数使用

 String[] keys={"input1","input2"};
            String[] values={input1,input2};
            params(call, keys, values);
String ret = (String) call.invoke(values);
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值