jdk11 调用 webservice maven 3.6.3为例

319 阅读1分钟

jdk11 调用 websercice

导入hutool pom

<!-- web service服务调用 -->
<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.8.8</version>
</dependency>

这里注意方法接口参数上 @WebParam 内需要加上 targetNamespace

package com.Tencent.cloud.service;

import javax.jws.WebParam;
import javax.jws.WebService;

@WebService(endpointInterface = "com.Tencent.cloud.service.UserService",
      targetNamespace="http://service.cloud.Tencent.com/UserService")
public interface UserService {
   
   /**
    * 用户登录
    * @param userAccount  账号
    * @param password 密码
    * @return
    */
   public String login(@WebParam(name = "userAccount",targetNamespace = "http://service.cloud.Tencent.com/UserService")String userAccount,
                  @WebParam(name = "password",targetNamespace = "http://service.cloud.Tencent.com/UserService")String password);


}

调用方法

package com.contract.draft.controller.thirdParty;


import cn.hutool.http.webservice.SoapClient;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Harper
 * @Description 描述
 * @date 2021/3/18 15:57
 */

public class WebServiceServerTest {
    public static void main(String[] args) {

        //wsdl文档地址
        String wsdlUrl = "http://localhost:8989/helloWs1111?wsdl";

        //创建soap客户端
        SoapClient soapClient = SoapClient.create(wsdlUrl)
                // // 这里的Method 参数为 调用方法名称 与 targetNamespace 地址
                .setMethod("user:login", "http://service.cloud.Tencent.com/UserService")
                .setParam("password",":111")
                .setParam("userAccount",":zhangsan")
                // 设置超时时间
                .setConnectionTimeout(15000)
                .setReadTimeout(15000)
                .timeout(15000);
        // 还有其他可以配置,根据自己的需求配置
        // 调用webservice接口
        String result = soapClient.send();
        // 打印
        System.out.println(result);

    }
}

如出现以下错误

image.png 尝试再导入以下pom后再进行尝试

<dependency>
    <groupId>com.sun.xml.ws</groupId>
    <artifactId>jaxws-ri</artifactId>
    <version>2.3.3</version>
</dependency>