【问题标题】:How to add Swagger related static files to Spring Boot + Jersey app?如何将 Swagger 相关的静态文件添加到 Spring Boot + Jersey 应用程序中?
【发布时间】:2018-04-17 15:26:14
【问题描述】:

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

我使用以下依赖项:

  • spring-boot-starter-parent:2.0.1.RELEASE
  • spring-boot-starter-jersey:2.0.1.RELEASE
  • swagger-jersey2-jaxrs:1.5.18

这是我的招摇配置:

@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-ui.html 时 Swagger 立即显示我的 REST API,用我在静态 Swagger 文件中的 URL 覆盖默认 swagger.json URL 的正确方法是什么。

【问题讨论】:

    标签: java spring-boot jersey swagger swagger-ui


    【解决方案1】:
    <dependency>
      <groupId>org.webjars</groupId>
      <artifactId>swagger-ui</artifactId>
      <version>${swagger-ui.version}</version>
    </dependency>
    

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

    【讨论】:

    【解决方案2】:

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

    我正在使用 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>
    

    第二步:添加配置类

    @Configuration
    @EnableSwagger2
    public class SwaggerConfig {
    
         public static final Contact DEFAULT_CONTACT = new Contact("Usama Amjad", "https://stackoverflow.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 中即可使用。

    【讨论】:

    • 这不是针对泽西岛的解决方案。
    猜你喜欢
    • 2014-11-10
    • 2020-09-13
    • 2013-11-06
    • 1970-01-01
    • 1970-01-01
    • 2017-03-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-18
    相关资源
    最近更新 更多