如何在 Spring 引导应用程序中将 apache cxf jax-rest api 中的 swagger 集成



如何在 Spring 引导应用程序中集成 apache cxf jax-rest API 中的 swagger ui?

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

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

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

配置类:

功能配置.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();
    }
}

资源接口和实现:

您好资源.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);
...
}

你好资源.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");
    }
...
}

属性文件:

应用程序.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

最新更新