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

时间:2017-05-23 11:03:19

标签: java spring spring-boot cxf

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

1 个答案:

答案 0 :(得分:4)

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

已编辑:刚刚发布了Implementing APIs using Spring Boot, CXF and Swagger

<强>的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>

主要课程:

<强> DemoCxfApplication.java

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

    public static void main(String[] args) {
        SpringApplication.run(DemoCxfApplication.class, args);
    }
}

配置类:

<强> 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;
    }
}

<强> ProvidersConfig.java

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

    @Bean
    public JacksonJsonProvider jsonProvider() {
        return new JacksonJsonProvider();
    }
}

资源界面和实施:

<强> 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);
...
}

<强> 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");
    }
...
}

属性文件:

<强> application.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

Swagger UI位于:http://<host:port>/api/api-docs?url=/api/swagger.json

WADL位于:http://<host:port>/api?_wadl