java - 如何将 swagger 与 jersey + spring-boot 整合

标签 java spring-boot jersey swagger swagger-ui

我正在使用 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 是用于 swagger 的?我试过 http://localhost:8080 , http://localhost:8080/swagger , 和 http://localhost:8080/swagger-ui.html .但是他们都无法访问。

最佳答案

我认为如果使用 Spring MVC 而不是 JAX-RS 实现来实现端点,则 @EnableSwagger2 注释和 springfox 依赖项将起作用。

我几个月前写过关于这个的博客,Microservices using Spring Boot, Jersey Swagger and Docker

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

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

@SpringBootApplication(
    scanBasePackages = {
        "com.asimio.jerseyexample.config", "com.asimio.jerseyexample.rest"
    }
)

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

3) 使用 JAX-RS (Jersey) 和 Swagger 注释的资源实现:

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

4) 确保您应用的 Spring Boot 配置文件区分 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
...

关于java - 如何将 swagger 与 jersey + spring-boot 整合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35966204/

相关文章:

java - 即使 java 中的字符串相同, equalsIgnoreCase 也会返回 false

java - 使用 DocumentFilter 后将文本附加到 JTextArea

java - Spring-boot 中的嵌套 JSON 对象反序列化

java - RestTemplate 抛出通用 400 错误请求,但自定义服务器发送的消息不会丢失

java - Spring Boot 应用程序在部署到 Tomcat 时会出现 404,但适用于嵌入式服务器

java - 找不到 Maven Webjars

java - 如何使用新的 JUnit 4.11 功能,包括更改测试名称和设置执行顺序

java - 从单独的方法使用 java awt drawRect 吗?

jersey - JAX-RS Jersey - 如何强制响应 ContentType?覆盖内容协商

Jersey ,@QueryParam 列表<字符串>