Java 如何将swagger与jersey融合+;弹簧靴

Java 如何将swagger与jersey融合+;弹簧靴,java,spring-boot,jersey,swagger,swagger-ui,Java,Spring Boot,Jersey,Swagger,Swagger Ui,我使用springboot+jersey实现web restful。现在我将把swagger集成到我们的应用程序中。我没有跟随 @Configuration @EnableSwagger2 public class JerseyConfiguration extends ResourceConfig { public JerseyConfiguration(){ register(HelloworldAPI.class); configureSwagge

我使用springboot+jersey实现web restful。现在我将把swagger集成到我们的应用程序中。我没有跟随

@Configuration
@EnableSwagger2
public class JerseyConfiguration extends ResourceConfig {

    public JerseyConfiguration(){
        register(HelloworldAPI.class);
        configureSwagger();
    }

    private void configureSwagger() {
        BeanConfig beanConfig = new BeanConfig();
        beanConfig.setVersion("1.0.2");
        beanConfig.setSchemes(new String[]{"http"});
        beanConfig.setHost("localhost:8080");
        beanConfig.setBasePath("/");
        beanConfig.setResourcePackage("com.cooltoo.api");
        beanConfig.setPrettyPrint(true);
        beanConfig.setScan(true);
    }
}
我在build.gradle上添加了以下依赖项:

compile('io.springfox:springfox-swagger2:'+springfoxSwaggerVersion)
compile('io.springfox:springfox-petstore:'+springfoxSwaggerVersion)
compile('io.springfox:springfox-swagger-ui:'+springfoxSwaggerVersion)
compile('io.swagger:swagger-jersey2-jaxrs:1.5.8')

我能够启动这个web应用程序,但我不知道哪个url是用来招摇过市的?我试过了,然后。但是没有一个可以访问

也许您使用的是相当旧的springfox版本,但现在(对于2.4.0版),它的配置必须与您的代码非常不同,请参阅

例如,我有以下springfox配置:

@Configuration
@EnableSwagger2
class SwaggerConfig {
    @Bean
    Docket rsApi() {
        new Docket(DocumentationType.SWAGGER_12)
            .apiInfo(apiInfo())
            .select()
                    .apis(RequestHandlerSelectors.basePackage('com.test.service'))
            .paths(PathSelectors.any())
            .build()
            .pathMapping('/')
            .useDefaultResponseMessages(false)
    }

    private ApiInfo apiInfo() {
        new ApiInfoBuilder()
            .title('Test API')
            .description('Test API')
            .version('0.0.10.SNAPSHOT')
            .termsOfServiceUrl('')
            .contact('Test company')
            .license('Public')
            .licenseUrl('http://example.com/')
            .build()
    }
}

我认为如果端点使用SpringMVC而不是JAX-RS实现,那么
@EnableSwagger2
注释和
springfox
依赖项将起作用

几个月前我在博客上写过

基本上,如果需要记录Jersey实现的端点,则需要:

(一) 确保您的Spring Boot应用程序通过以下方式扫描特定软件包(即com.asimio.jerseyexample.config)中的组件:

2) Jersey配置类实现:

package com.asimio.jerseyexample.config;
...
@Component
public class JerseyConfig extends ResourceConfig {

    @Value("${spring.jersey.application-path:/}")
    private String apiPath;

    public JerseyConfig() {
        // Register endpoints, providers, ...
        this.registerEndpoints();
    }

    @PostConstruct
    public void init() {
        // Register components where DI is needed
        this.configureSwagger();
    }

    private void registerEndpoints() {
        this.register(HelloResource.class);
        // Access through /<Jersey's servlet path>/application.wadl
        this.register(WadlResource.class);
    }

    private void configureSwagger() {
        // Available at localhost:port/swagger.json
        this.register(ApiListingResource.class);
        this.register(SwaggerSerializers.class);

        BeanConfig config = new BeanConfig();
        config.setConfigId("springboot-jersey-swagger-docker-example");
        config.setTitle("Spring Boot + Jersey + Swagger + Docker Example");
        config.setVersion("v1");
        config.setContact("Orlando L Otero");
        config.setSchemes(new String[] { "http", "https" });
        config.setBasePath(this.apiPath);
        config.setResourcePackage("com.asimio.jerseyexample.rest.v1");
        config.setPrettyPrint(true);
        config.setScan(true);
    }
}
4) 确保应用程序的Spring引导配置文件区分Spring MVC(用于执行器端点)和Jersey(用于资源)端点:

application.yml

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

您的应用程序是否有上下文根?相对于上下文根,它应该在swagger-ui.html上可用。上下文根只是“/”。这适用于SpringMVC,而不是Jersey(jax-rs)。正如我们在这里看到的:我们可以确认这一点。看起来至少@Api注释是在扫描期间获取资源所必需的。要从jersey+spring boot应用程序提供静态招摇ui,请参阅
package com.asimio.jerseyexample.rest.v1;
...
@Component
@Path("/")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Api(value = "Hello resource", produces = "application/json")
public class HelloResource {

    private static final Logger LOGGER = LoggerFactory.getLogger(HelloResource.class);

    @GET
    @Path("v1/hello/{name}")
    @ApiOperation(value = "Gets a hello resource. Version 1 - (version in URL)", response = Hello.class)
    @ApiResponses(value = {
        @ApiResponse(code = 200, message = "Hello resource found"),
        @ApiResponse(code = 404, message = "Hello resource not found")
    })
    public Response getHelloVersionInUrl(@ApiParam @PathParam("name") String name) {
        LOGGER.info("getHelloVersionInUrl() v1");
        return this.getHello(name, "Version 1 - passed in URL");
    }
...
}
...
# Spring MVC dispatcher servlet path. Needs to be different than Jersey's to enable/disable Actuator endpoints access (/info, /health, ...)
server.servlet-path: /
# Jersey dispatcher servlet
spring.jersey.application-path: /api
...