首页 文章

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

提问于
浏览
-2

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

1 回答

  • 3

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

    编辑:刚出版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

相关问题