<dependencies>
<dependency>
<groupId>com.valuegroupId>
<artifactId>commonartifactId>
dependency>
<dependency>
<groupId>org.apache.cxfgroupId>
<artifactId>cxf-spring-boot-starter-jaxwsartifactId>
<version>${cxf.version}version>
dependency>
dependencies>
server:
tomcat:
uri-encoding: UTF-8
spring:
application:
name: jaxws
cxf:
path: /
servlet:
load-on-startup: 1
@Configuration
@ConditionalOnClass(CXFServlet.class)
public class JaxWsConfig {
@Autowired
CxfAutoConfiguration cxfAutoConfiguration;
@Autowired
private SpringBus bus;
/**
* 这里也可以使用服务端点方式或CXFServlet发布服务
*/
/*
@Bean
public Endpoint printerpEndpoint(){
EndpointImpl endpoint = new EndpointImpl(bus, helloService());
endpoint.publish("/api");
endpoint.getFeatures().add(new LoggingFeature());
return endpoint;
}*/
/* @Bean
public ServletRegistrationBean cxfServletRegistration(){
return new ServletRegistrationBean(new CXFServlet(), "/soap/*");
}*/
@Bean
public HelloService helloService(){
return new HelloServiceImpl();
}
@Bean
public Server jaxWsServer(){
JaxWsServerFactoryBean wsEndpoint = new JaxWsServerFactoryBean();
wsEndpoint.setBus(bus);
wsEndpoint.setAddress("/soap/");
wsEndpoint.setServiceClass(HelloService.class);
wsEndpoint.setServiceBean(helloService());
wsEndpoint.setFeatures(Arrays.asList(new LoggingFeature()) );
return wsEndpoint.create();
}
}
/**
* 服务接口
*/
@WebService(name = "HelloService" ,
targetNamespace = "http://soap.value.com")
public interface HelloService {
@WebMethod(action = "sayHello")
@WebResult(name = "response", partName = "response")
@XmlElement(required = true)
String sayHello(@WebParam(name = "a", partName = "a") String a);
}
/**
* 服务实现
*/
@SOAPBinding(style = SOAPBinding.Style.RPC,use = SOAPBinding.Use.LITERAL)
@WebService(serviceName = "HelloService",targetNamespace = "http://soap.value.com", endpointInterface = "com.value.jaxws.service.HelloService")
public class HelloServiceImpl implements HelloService {
@Override
public String sayHello(String a) {
return "Hello Springboot Integrated CXF JaxWS : " + a;
}
}
<dependencies>
<dependency>
<groupId>com.valuegroupId>
<artifactId>commonartifactId>
dependency>
<dependency>
<groupId>org.apache.cxfgroupId>
<artifactId>cxf-spring-boot-starter-jaxrsartifactId>
<version>${cxf.version}version>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
dependency>
<dependency>
<groupId>org.apache.cxfgroupId>
<artifactId>cxf-rt-rs-extension-providersartifactId>
<version>${cxf.version}version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>fastjsonartifactId>
<version>${fastjson.version}version>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.jaxrsgroupId>
<artifactId>jackson-jaxrs-json-providerartifactId>
dependency>
dependencies>
server:
tomcat:
uri-encoding: UTF-8
spring:
application:
name: jaxrs
jackson:
#日期格式化
date-format: yyyy-MM-dd HH:mm:ss
serialization:
#格式化输出
indent_output: true
#忽略无法转换的对象
fail_on_empty_beans: false
#序列化带根对象
wrap_root_value: true
#设置空如何序列化
defaultPropertyInclusion: NON_EMPTY
deserialization:
#允许对象忽略json中不存在的属性
fail_on_unknown_properties: false
#反序列化带根对象
unwrap_root_value: true
parser:
#允许出现特殊字符和转义符
allow_unquoted_control_chars: true
#允许出现单引号
allow_single_quotes: true
cxf:
path: /
@Configuration
public class JaxRsConfig {
@Autowired
private SpringBus bus;
@Autowired
private ObjectMapper objectMapper;
@Bean
public Server jaxRsServer() {
JAXRSServerFactoryBean rsEndpoint = new JAXRSServerFactoryBean();
rsEndpoint.setBus(bus);
rsEndpoint.setAddress("/rest/");
rsEndpoint.setProviders(Arrays.asList(jacksonJaxbJsonProvider()));
rsEndpoint.setFeatures(Arrays.asList(new LoggingFeature()));
rsEndpoint.setServiceBeans(Collections.singletonList(new HelloServiceImpl()));
return rsEndpoint.create();
}
/**
* 配置一个对象与json转换的工具
*/
@Bean
public JacksonJaxbJsonProvider jacksonJaxbJsonProvider() {
return new JacksonJaxbJsonProvider(objectMapper, JacksonJaxbJsonProvider.DEFAULT_ANNOTATIONS);
}
}
/**
* 服务接口
*/
@Path("/sayHello")
public interface HelloService {
@GET
@Path("/info")
@Consumes(MediaType.TEXT_PLAIN)
@Produces(MediaType.TEXT_PLAIN)
String sayHello(@QueryParam("a") String a);
}
/**
* 服务实现
*/
@Service
@Slf4j
public class HelloServiceImpl implements HelloService {
@Override
public String sayHello(String a) {
return "Hello Springboot Integrated CXF JaxRS : " + a;
}
}
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-actuatorartifactId>
dependency>
<dependency>
<groupId>com.valuegroupId>
<artifactId>jaxwsartifactId>
dependency>
<dependency>
<groupId>com.valuegroupId>
<artifactId>jaxrsartifactId>
dependency>
dependencies>
server:
port: 8080
tomcat:
uri-encoding: UTF-8
servlet:
context-path: /datasync
spring:
application:
name: value-datasync
profiles:
active: dev
include: jaxws, jaxrs
@SpringBootApplication
public class SpringbootCxfApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootCxfApplication.class,args);
}
}
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-PO5yTNOv-1620698642958)(C:\Users\yinchen\AppData\Roaming\Typora\typora-user-images\image-20210511095759567.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-MJxj8zgA-1620698642973)(C:\Users\yinchen\AppData\Roaming\Typora\typora-user-images\image-20210511100051620.png)]