如何将 Swagger 相关静态文件添加到 Spring Boot + Jersey 应用程序?

How to add Swagger related static files to Spring Boot + Jersey app?

我正在尝试将 Swagger 支持添加到我的 REST API,但我对如何将 Swagger 相关的静态内容(HTML、JS)文件添加到我的 Spring 启动应用程序感到困惑.

我使用了以下依赖:

这是我的招摇配置:

@Configuration
public class SwaggerConfig {
    @Bean
    public BeanConfig swaggerConfiguration() {
        final BeanConfig beanConfig = new BeanConfig();
        beanConfig.setResourcePackage("a.b.c");
        beanConfig.setScan(true);
        beanConfig.setPrettyPrint(true);
        return beanConfig;
    }
}

以及球衣配置:

@Component
public class JerseyConfig extends ResourceConfig {
    public JerseyConfig() {
        register(ImageResource.class);
        register(io.swagger.jaxrs.listing.ApiListingResource.class);
        register(io.swagger.jaxrs.listing.SwaggerSerializers.class);
    }
}

这部分就像一个魅力,当我打开 http://localhost:8090/swagger.json 然后我可以看到预期的 Swagger JSON 内容。

但我不知道如何将与 Swagger 相关的静态 HTML 内容添加到我的应用程序中。我可以看到这个内容在 springfox-swagger-ui.jar 中,我可以将它作为 Maven 依赖项添加到我的项目中,但是我如何从这个 jar 中解压内容?

在静态 Swagger 文件中用我的 URL 覆盖默认 swagger.json URL 的正确方法是什么,以便 Swagger 立即显示我的 REST API当我打开 swagger-ui.html.

<dependency>
  <groupId>org.webjars</groupId>
  <artifactId>swagger-ui</artifactId>
  <version>${swagger-ui.version}</version>
</dependency>

请不要包含 springfox-swagger-ui.jar,它适用于 SpringRestController

您现在一定已经解决了,但它可能会对其他人有所帮助,所以这是完整的过程,因为我也在寻找教程。

我正在将 Swagger V2Spring Boot 2 一起使用,这是一个简单的 3 步过程。

步骤 1:pom.xml 文件中添加所需的依赖项。第二个依赖项是可选的,仅当您需要 Swagger UI.

时才使用它
        <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>

第 2 步: 添加配置 class

@Configuration
@EnableSwagger2
public class SwaggerConfig {

     public static final Contact DEFAULT_CONTACT = new Contact("Usama Amjad", "https://whosebug.com/users/4704510/usamaamjad", "hello@email.com");
      public static final ApiInfo DEFAULT_API_INFO = new ApiInfo("Article API", "Article API documentation sample", "1.0", "urn:tos",
              DEFAULT_CONTACT, "Apache 2.0", "http://www.apache.org/licenses/LICENSE-2.0", new ArrayList<VendorExtension>());

    @Bean
    public Docket api() {
        Set<String> producesAndConsumes = new HashSet<>();
        producesAndConsumes.add("application/json");
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(DEFAULT_API_INFO)
                .produces(producesAndConsumes)
                .consumes(producesAndConsumes);

    }
}

第 3 步:设置完成,现在您需要在 controllers

中记录 API
    @ApiOperation(value = "Returns a list Articles for a given Author", response = Article.class, responseContainer = "List")
    @ApiResponses(value = { @ApiResponse(code = 200, message = "Success"),
            @ApiResponse(code = 404, message = "The resource you were trying to reach is not found") })
    @GetMapping(path = "/articles/users/{userId}")
    public List<Article> getArticlesByUser() {
       // Do your code
    }

用法:

Swagger UI: 您可以通过 http://localhost:8080/swagger-ui.html

访问它

Postman: 您还可以从 http://localhost:8080/v2/api-docs 访问您的文档 JSON,只需将其复制粘贴到 Postman 中即可使用。