首页 文章

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

提问于
浏览
1

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

I use the following dependencies:

  • spring-boot-starter-parent:2.0.1.RELEASE

  • spring-boot-starter-jersey:2.0.1.RELEASE

  • swagger-jersey2-jaxrs:1.5.18

This is my swagger configuration:

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

And the jersey configuration:

@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-ui.html 时Swagger立即显示我的REST API .

2 回答

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

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

  • 0

    你现在必须解决它,但它可能会帮助其他人所以这里是完整的程序,因为我也在寻找一个教程 .

    我正在使用 Swagger V2Spring Boot 2 这是一个简单的3步骤过程 .

    Step 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>
    

    Step 2: 添加配置类

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

    Step 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
        }
    

    Usage:

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

    enter image description here

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

    enter image description here

相关问题