如何在 Spring Boot 应用程序中将 swagger 集成到 apache cxf jax-rest api 中?

Pra*_*mar -2 java spring cxf spring-boot

如何在 Spring Boot 应用程序中将 swagger ui 集成到 apache cxf jax-rest api 中?

oot*_*ero 5

我正在编辑一篇带有演示的博客文章,实际上演示已经完成,关于这个确切的主题,这里是源代码的摘录:

编辑:刚刚发布使用 Spring Boot、CXF 和 Swagger 实现 API

pom.xml

<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-spring-boot-starter-jaxrs</artifactId>
    <version>${cxf.version}</version>
</dependency>
<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-rs-service-description</artifactId>
    <version>${cxf.version}</version>
</dependency>
<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-rs-service-description-swagger</artifactId>
    <version>${cxf.version}</version>
</dependency>
<dependency>
    <groupId>org.webjars</groupId>
    <artifactId>swagger-ui</artifactId>
    <version>${swagger-ui.version}</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.jaxrs</groupId>
    <artifactId>jackson-jaxrs-json-provider</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)

主要类别:

演示CxfApplication.java

@SpringBootApplication(scanBasePackages = { "com.example.demo.rest" })
public class DemoCxfApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoCxfApplication.class, args);
    }
}
Run Code Online (Sandbox Code Playgroud)

配置类:

FeaturesConfig.java

package com.example.demo.rest.config;
...
@Configuration
public class FeaturesConfig {

    @Value("${cxf.path}")
    private String basePath;

    @Bean("swagger2Feature")
    public Feature swagger2Feature() {
        Swagger2Feature result = new Swagger2Feature();
        result.setTitle("Spring Boot + CXF + Swagger + Docker Example");
        result.setDescription("Spring Boot + CXF + Swagger + Docker Example description");
        result.setBasePath(this.basePath);
        result.setVersion("v1");
        result.setSchemes(new String[] { "http", "https" });
        result.setPrettyPrint(true);
        return result;
    }
}
Run Code Online (Sandbox Code Playgroud)

ProvidersConfig.java

package com.example.demo.rest.config;
...
@Provider
@Configuration
public class ProvidersConfig {

    @Bean
    public JacksonJsonProvider jsonProvider() {
        return new JacksonJsonProvider();
    }
}
Run Code Online (Sandbox Code Playgroud)

资源接口及实现:

HelloResource.java

package com.example.demo.rest.v1;
...
@Path("/")
@Api(value = "Hello resource Version 1", consumes = MediaType.APPLICATION_JSON, produces = MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public interface HelloResource {

    @GET
    @Path("v1/hello/{name}")
    @ApiOperation(value = "Gets a hello resource. Version 1 - (version in URL)")
    @ApiResponses(value = { 
        @ApiResponse(code = 200, message = "Hello resource found", response = Hello.class),
        @ApiResponse(code = 404, message = "Hello resource not found")
    })
    Response getHelloVersionInUrl(@PathParam("name") @ApiParam(value = "The name") String name);
...
}
Run Code Online (Sandbox Code Playgroud)

HelloResourceImpl.java

package com.example.demo.rest.v1.impl;
...
// No JAX-RS annotation in class, method or method arguments
@Component("helloResourceV1")
public class HelloResourceImpl implements HelloResource {

    @Override
    public Response getHelloVersionInUrl(String name) {
        LOGGER.info("getHelloVersionInUrl() v1");
        return this.getHello(name, "Version 1 - passed in URL");
    }
...
}
Run Code Online (Sandbox Code Playgroud)

属性文件:

应用程序.yml

# Spring MVC dispatcher servlet path. Needs to be different than CXF's to enable/disable Actuator endpoints access (/info, /health, ...)
server.servlet-path: /

management.security.enabled: false

# http://cxf.apache.org/docs/springboot.html#SpringBoot-SpringBootCXFJAX-RSStarter
cxf:
  path: /api # CXFServlet URL pattern
  jaxrs:
    component-scan: true
Run Code Online (Sandbox Code Playgroud)

Swagger UI可以在:http://<host:port>/api/api-docs?url=/api/swagger.json

WADL可以在:http://<host:port>/api?_wadl