当前位置:   article > 正文

Hutool SoapClient 调用使用@webservice 发布的webService接口,参数传递为空_hutusoapclient使用

hutusoapclient使用

一.发布webService接口

(1)接口声明

  1. import javax.jws.WebService;
  2. @WebService
  3. public interface Calculator {
  4. String add(String a, String b);
  5. int multi(int a, int b);
  6. }

(2)实现方法

 

  1. import com.maxnerva.cloud.webservice.service.Calculator;
  2. import javax.jws.WebService;
  3. public class CalculatorImpl implements Calculator {
  4. @Override
  5. public String add(String a, String b) {
  6. return a + b;
  7. }
  8. @Override
  9. public int multi(int a, int b) {
  10. return a * b;
  11. }
  12. }

(3) 发布

  1. public class Test {
  2. public static void main(String[] args) {
  3. Endpoint.publish("http://127.0.0.1:8088/calculator",
  4. new CalculatorImpl());
  5. }
  6. }

二.hutool SoapClient 访问

验证add方法

1.Web 访问:

2.调用

  1. import cn.hutool.http.webservice.SoapClient;
  2. public class TestImpl {
  3. public static void main(String[] args) {
  4. SoapClient soapClient = SoapClient.create("http://127.0.0.1:8088/calculator?wsdl");
  5. soapClient.setMethod("add", "http://service.webservice.cloud.maxnerva.com/").setParam("a", "1").setParam("b","1");
  6. String send = soapClient.send(true);
  7. System.out.println(send);
  8. }
  9. }

3.执行报错: 所需元素为<{}arg1>,<{}arg0&gt

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <soap:Fault>
      <faultcode>soap:Client</faultcode>
      <faultstring>Unmarshalling Error: 意外的元素 (uri:"http://service.webservice.cloud.maxnerva.com/", local:"a")。所需元素为<{}arg1>,<{}arg0> </faultstring>
    </soap:Fault>
  </soap:Body>
</soap:Envelope>

三.解决方案

安装SoapUI工具

将测试的webservice 链接加入进去可以看到具体方法体,如图,其中方法名在webService 里面变成了"ser:add";参数变为了"arg0","arg1":

重新修改验证调用方法为:

  1. import cn.hutool.http.webservice.SoapClient;
  2. public class TestImpl {
  3. public static void main(String[] args) {
  4. SoapClient soapClient = SoapClient.create("http://127.0.0.1:8088/calculator?wsdl");
  5. soapClient.setMethod("ser:add", "http://service.webservice.cloud.maxnerva.com/")
  6. .setParam("arg0", "1", false)
  7. .setParam("arg1","1", false);
  8. String send = soapClient.send(true);
  9. System.out.println(send);
  10. }
  11. }

验证结果,成功:

注意参数设置

setParam(String name, Object value, boolean useMethodPrefix) 其中useMethodPrefix为fasle,是不使用方法名的前缀,在这个示例种,方法add的前缀为“ser”

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <ns2:addResponse xmlns:ns2="http://service.webservice.cloud.maxnerva.com/">
      <return>11</return>
    </ns2:addResponse>
  </soap:Body>
</soap:Envelope>

四,注意:

使用 @webservic发布webService 接口的时候,方法名或者参数可能和定义有区别,可借助SoapUI工具查看

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
  

闽ICP备14008679号