springboot+Cxf+JaxRs创建一个完整的web service项目
时间: 2023-12-24 15:04:36 浏览: 136
下面是使用Spring Boot、CXF和JAX-RS创建一个完整的Web Service项目的步骤:
1. 创建一个Maven项目,并添加以下依赖项到pom.xml文件中:
```xml
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxrs</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
```
2. 创建一个JAX-RS服务类:
```java
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/hello")
public class HelloService {
@GET
@Produces(MediaType.TEXT_PLAIN)
public String sayHello() {
return "Hello, World!";
}
}
```
3. 创建一个CXF配置类:
```java
import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
import org.apache.cxf.jaxrs.swagger.Swagger2Feature;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.Arrays;
@Configuration
public class CxfConfig {
@Bean
public JAXRSServerFactoryBean jaxrsServerFactoryBean() {
JAXRSServerFactoryBean factory = new JAXRSServerFactoryBean();
factory.setAddress("/");
factory.setServiceBeans(Arrays.<Object>asList(new HelloService()));
// Swagger2
factory.setFeatures(Arrays.asList(new Swagger2Feature()));
return factory;
}
}
```
4. 创建一个Spring Boot应用程序:
```java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Import;
@SpringBootApplication
@Import({CxfConfig.class})
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
```
5. 运行应用程序并访问Web服务:
启动应用程序后,在浏览器中访问http://localhost:8080/hello,应该会看到“Hello, World!”的文本。
以上就是使用Spring Boot、CXF和JAX-RS创建一个完整的Web Service项目的方法。
阅读全文