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>