摘要
说起web service最近几年restful大行其道,大有取代传统soap web service的趋势,但是一些特有或相对老旧的系统依然使用了传统的soap web service,例如银行、航空公司的机票查询接口等。
目前就遇到了这种情况,需要在系统中查询第三方提供的soap web service接口,也就是说要将它整合进现有的系统当中。
spring整合CXF本来十分简单,但是因为使用了Spring boot
,不想用以前xml一堆配置的方式,那么能否按照Spring boot的风格优雅的进行整合呢?
答案当然是肯定的,但是遍查网上几乎没有这方面的资料,折腾过后觉得还是有必要记录一下,虽然它显得非常的简单。
添加依赖
<!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-spring-boot-starter-jaxws --><dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
<version>3.1.12</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-transports-http-jetty -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>3.1.12</version>
</dependency>
业务代码
/**
* 接口
*
* @author refineli
*
*/
@WebService(name = "CommonService", // 暴露服务名称
targetNamespace = "http://webservice.refineli.org/"// 命名空间,一般是接口的包名倒序
)
public interface CommonService {
@WebMethod
@WebResult(name = "String", targetNamespace = "")
public String sayHello(@WebParam(name = "userName") String name);
}
/**
* 接口实现
*
* @author refineli
*
*/
@WebService(serviceName = "CommonService", // 与接口中指定的name一致
targetNamespace = "http://webservice.refineli.org/", // 与接口中的命名空间一致,一般是接口的包名倒
endpointInterface = "org.refineli.webservice.CommonService"// 接口地址
)
@Component
public class CommonServiceImp implements CommonService {
@Override
public String sayHello(String name) {
return "Hello ," + name;
}
}
发布服务
@Configuration
public class CxfConfig {
@Autowired
private Bus bus;
@Autowired
CommonService commonService;
/** JAX-WS **/
@Bean
public Endpoint endpoint() {
EndpointImpl endpoint = new EndpointImpl(bus, commonService);
endpoint.publish("/CommonService");
return endpoint;
}
}
spring boot整合cxf发布webservice服务和cxf客户端调用
public class CxfClient {
public static void main(String[] args) {
cl1();
System.out.println("###########################");
cl2();
System.out.println("###########################");
cl3();
}
/**
* 方式1.代理类工厂的方式,需要拿到对方的接口
*/
public static void cl1() {
try {
// 接口地址
String address = "http://localhost:8080/services/CommonService?wsdl";
// 代理工厂
JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
// 设置代理地址
jaxWsProxyFactoryBean.setAddress(address);
// 设置接口类型
jaxWsProxyFactoryBean.setServiceClass(CommonService.class);
// 创建一个代理接口实现
CommonService cs = (CommonService) jaxWsProxyFactoryBean.create();
// 数据准备
String userName = "Leftso";
// 调用代理接口的方法调用并返回结果
String result = cs.sayHello(userName);
System.out.println("返回结果:" + result);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 动态调用方式
*/
public static void cl2() {
// 创建动态客户端
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
Client client = dcf.createClient("http://localhost:8080/services/CommonService?wsdl");
// 需要密码的情况需要加上用户名和密码
// client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME,
// PASS_WORD));
Object[] objects = new Object[0];
try {
// invoke("方法名",参数1,参数2,参数3....);
objects = client.invoke("sayHello", "Leftso");
System.out.println("返回数据:" + objects[0]);
} catch (java.lang.Exception e) {
e.printStackTrace();
}
}
public static void cl3() {
Service service = Service.create(SERVICE_NAME);
// Endpoint Address
String endpointAddress = "http://localhost:8080/services/CommonService";
// Add a port to the Service
service.addPort(PORT_NAME, SOAPBinding.SOAP11HTTP_BINDING, endpointAddress);
CommonService cs = service.getPort(CommonService.class);
System.out.println(cs.sayHello("world"));
}
private static final QName SERVICE_NAME = new QName("http://webservice.refineli.org/", "CommonService");
private static final QName PORT_NAME = new QName("http://webservice.refineli.org/", "CommonServicePort");
}
源码地址:https://gitee.com/bbli/springboot-xcf/