CXF 3.5.x版本 客户端动态调用

CXF 3.5.x版本 客户端动态调用

按照官网的来就行
https://cxf.apache.org/index.html
根据官网可知最新的版本是3.5.2 ,但是官网没有提供maven的安装方式。
没关系,我们找一下阿里云仓库

很简单就搜索出来了。这里搜索官方的即可,最好别用第三方的
在这里插入图片描述
然后直接看官网的说明就好(虽然内容很乱)
https://cxf.apache.org/docs/how-do-i-develop-a-client.html#HowdoIdevelopaclient?-DynamicClient

https://cxf.apache.org/docs/dynamic-clients.html

这里简单说明一下调用步骤

1.创建dto

@Data
public class Department {
    private String name;
    private String organId;
    private List<Departmentlist> departmentlist;
    private String numDoctors;
}

2.写service业务层

        String synUserName="";
        String synKey="";
        String wsdlString = "https://6xxxxxxdc.cpolar.cn/LiuZhouWS/services/Common?wsdl";
        //创建客户端实例
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        // 加载指定的wsdl
        Client client = dcf.createClient(wsdlString); 
        //调用 queryDepartmentList 方法,
        Object[] res = client.invoke("queryDepartmentList",synUserName,synKey);
        log.info("queryDepartmentList response: " + res[0]);
        
        //将结果 序列化为dto
        DepartmentListDTO departmentListDTO = new DepartmentListDTO();
        departmentListDTO = JSONObject.parseObject(res[0].toString(), DepartmentListDTO.class);
        return departmentListDTO;

要注意的地方

1.make sure you save it prior to calling createClient.

在这里插入图片描述
The JaxWsDynamicClientFactory sets the Thread context ClassLoader to a new ClassLoader that contains the classes for the generated types. If you need the original ClassLoader, make sure you save it prior to calling createClient.

意思就是把 Client 全局变量存一下? 避免每一次都访问一遍wsdl?

2.要重置上下文

public class Test {
    public static void main(String[] args){
        //在一个方法中连续调用多次WebService接口,每次调用前需要重置上下文
        ClassLoader cl = Thread.currentThread().getContextClassLoader();

        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        System.out.println("学生的信息如下:================");
        printStudentList(dcf);
        //在调用第二个webservice前,需要重置上下文
        Thread.currentThread().setContextClassLoader(cl);

        System.out.println("用户的信息如下:================");
        printUserList(dcf);
    }

在以前的版本中

 <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
            <version>3.1.12</version>
        </dependency>

没有 Thread.currentThread().setContextClassLoader(cl);的话,是会报错的
在这里插入图片描述
但是在3.5.2的版本中。好像不写这个也成功

    <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>apache-cxf</artifactId>
            <version>3.5.2</version>
            <type>pom</type>
        </dependency>
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

何浩翔

如果对你多帮助,请支持。感谢!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值