1.关于springboot 如何接入cxf的文章并不少,本篇文章主侧重于springboot1.X与spirngboot2.X如何整合cxf,下面是代码实现
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mrb</groupId>
<artifactId>miniPJ</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
<version>3.1.11</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.18</version>
<type>jar</type>
</dependency>
</dependencies>
</project>
application.properties
server.port=8700
Application.java
package com.mrb.minipj;
import org.apache.cxf.Bus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import javax.jws.WebService;
import javax.xml.ws.Endpoint;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author MRB
*/
@SpringBootApplication
public class Application {
public static void main(String args[]){
SpringApplication.run(Application.class, args);
}
@Configuration
public static class CxfConfig {
@Autowired
private Bus bus;
@Autowired
public DemoService demoService;
@Bean
public Endpoint endpoint() {
EndpointImpl endpoint = new EndpointImpl(bus, demoService);
WebService serviceAnn = demoService.getClass().getDeclaredAnnotation(WebService.class);
endpoint.publish("/"+serviceAnn.name());
return endpoint;
}
}
@Component
@WebService(serviceName = "demo",
targetNamespace = "http://service.minipj.mrb.com"
)
public static class DemoService {
public String helloWorld(String param) {
return "hello"+param;
}
}
}
2.运行Application.main,访问:http://localhost:8700/services/centerwms?wsdl 结果如下
3.以上就能快速启动一个WebService demo,但这个是springboot1.4.4版本启动的,下表是
cxf-spring-boot-starter-jaxws 对应 springboot版本
cxf-spring-boot-starter-jaxws | springboot |
| 1.4.X |
3.2.X | 1.5.X |
3.3.X | 2.X |
①将 cxf-spring-boot-starter-jaxws 版本 切换为 3.2.9:
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
<version>3.2.9</version>
</dependency>
程序是能够正常运行。
②将 cxf-spring-boot-starter-jaxws 版本 切换为 3.3.0:
回报类没有定义的错误:Caused by: java.lang.NoClassDefFoundError: javax/validation/ClockProvider
原因在于 cxf-spring-boot-starter-jaxws 默认依赖java.validation:validation-api:1.1.0的这个版本没有没有定义这个类。
解决方法:
直接添加<parent> 依赖,然后重新编译
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.2.RELEASE</version>
</parent>
可以看到这个版本切换了:
点击运行,访问:http://localhost:8700/services/centerwms?wsdl 结果如下