栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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;
    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);
    }

在以前的版本中

 
            org.apache.cxf
            cxf-spring-boot-starter-jaxws
            3.1.12
        

没有 Thread.currentThread().setContextClassLoader(cl);的话,是会报错的

但是在3.5.2的版本中。好像不写这个也成功

    
            org.apache.cxf
            apache-cxf
            3.5.2
            pom
        
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/895005.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号