Spring Boot + CXF实践
Spring Boot + CXF实践
·
在Spring Boot(2.5.4)中集成CXF发布web服务(jax-ws)
步骤1:添加Maven依赖
因为cxf starter依赖版本的问题,踩过好几个坑,大家如果遇到没有明确解决线索的问题,可以尝试用最新版本试试。
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
<version>3.4.4</version>
</dependency>
步骤2:定义业务接口
实测接口层是可以去掉的
package com.etoak.ws;
import javax.jws.WebService;
@WebService(name = "Hello01", targetNamespace = "http://ws.etoak.com")
public interface IWsBusi01 {
String sayHello01();
}
步骤3:定义业务实现类
package org.etoak;
import com.etoak.ws.IWsBusi01;
import javax.jws.WebService;
/**
* Hello world!
*
*/
@WebService(serviceName = "Hello01", targetNamespace = "http://ws.etoak.com", endpointInterface = "com.etoak.ws.IWsBusi01")
public class WsBusi01Impl implements IWsBusi01 {
@Override
public String sayHello01() {
System.out.println(">>>>>>>>>sayHello01");
return "sayHello01";
}
}
步骤4:发布Web服务
@Configuration
public class WebServiceConfig {
@Autowired
private Bus bus;
@Bean
public Endpoint endpoint1() {
EndpointImpl endpoint = new EndpointImpl(bus, new WsBusi01Impl());
//Web服务名称
endpoint.publish("/Hello01");
return endpoint;
}
@Bean
public Endpoint endpoint2() {
EndpointImpl endpoint = new EndpointImpl(bus, new WsBusi02Impl());
endpoint.publish("/Hello02");
return endpoint;
}
/*@Bean (name = "cxfServlet")
public ServletRegistrationBean cxfServlet() {
return new ServletRegistrationBean(new CXFServlet(), "/webservice/*");
}
@Bean (name = Bus.DEFAULT_BUS_ID)
public SpringBus getSpringBus() {
return new SpringBus();
}
// 手动发布
@Bean
public EndpointImpl serviceEndpoint() {
EndpointImpl endpoint = null;
// 获取配置的需要发布服务的列表
// ...
DefaultListableBeanFactory defaultListableBeanFactory = (DefaultListableBeanFactory) applicationContext.getAutowireCapableBeanFactory();
for (迭代配置的服务列表) {
// 实现类的全限定名称
String clazzName;
// 配置的自定义服务名
String webServiceAnnotationName;
try {
Class<?> clazz = Class.forName(clazzName);
Object o = clazz.newInstance();
// 判断是否有@WebService注解
WebService webServiceAnnotation = clazz.getAnnotation(WebService.class);
if (webServiceAnnotation != null) {
if (StringUtils.hasLength(webServiceAnnotationName)) {
BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(EndpointImpl.class);
endpoint = new EndpointImpl((SpringBus) applicationContext.getBean(Bus.DEFAULT_BUS_ID), o);
endpoint.getInInterceptors().add(authInterceptor);
endpoint.publish(webServiceAnnotationName);
defaultListableBeanFactory.registerBeanDefinition(webServiceAnnotationName, beanDefinitionBuilder.getBeanDefinition());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
return endpoint;
}*/
}
步骤5:启动Spring Boot
访问:http://localhost:8080/services/Hello01?wsdl
服务发布成功
===============================================
实际上当时业务需求要稍微复杂一点,拆拆成了SpringBoot工程和业务工程,SpringBoot工程作为单独的Web应用启动,加载每一个业务工程jar包。所以在打包SpringBoot工程的时候,需要将第三方jar包分离出来,具体实现参考《关于打包jar/war_wsdhla的专栏-CSDN博客》
再加上XXL,最终架构如下:
==============================================
配置文件方式:
1 增加配置文件application-cxf.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
<jaxws:endpoint implementor="com.xxx.XxxImpl" address="/demoServ"/>
</beans>
2 启动类读取配置文件
@ImportResource(locations = {"classpath:application-cxf.xml"})
更多推荐
已为社区贡献1条内容
所有评论(0)