【问题标题】:Swagger with spring MVC not generating the documentation of the services使用 spring MVC 招摇不生成服务文档
【发布时间】:2017-05-31 12:33:34
【问题描述】:

我正在使用带有 spring mvc 的招摇,pom.xml

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.6.1</version>
</dependency>

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.6.1</version>
</dependency>

在spring configuration.xml中

<mvc:resources mapping="swagger-ui.html" location="classpath:/META-INF/resources/" />
<mvc:resources mapping="/webjars/**"
    location="classpath:/META-INF/resources/webjars/" />

我也使用基于 java 的配置

@Configuration
@EnableSwagger2
@PropertySource("classpath:application.properties")
public class SwaggerConfig extends WebMvcConfigurerAdapter {

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.regex("/api/.*")).build().apiInfo(apiInfo());
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().title(" Admin Api").description("Admin Api").version("V1")
                .termsOfServiceUrl("http://terms-of-services.url").license("LICENSE")
                .licenseUrl("http://url-to-license.com").build();
    }
}

在我使用的控制器中

@Controller
@RequestMapping({ "/" })
@Api(value = "product", description = "Products Web Services") // Swagger annotation
public class ProductController {
    @ApiOperation(value = "products", nickname = "Get list of all products", response = ProductListResponse.class)
    @RequestMapping(value = "/products", method = RequestMethod.GET)
    public @ResponseBody ProductListResponse getAllProducts(HttpServletResponse httpServletResponse) {

application.propeties 我添加了springfox.documentation.swagger.v2.path=/api-docs。 这就是我用来生成文档的所有信息 使用 url http://localhost:8080/admin-api/admin/api-docs ,它不会为端点生成文档

{
swagger: "2.0",
info: {
description: " Admin Api",
version: "V1",
title: "Admin Api",
termsOfService: "http://terms-of-services.url",
license: {
name: "LICENSE",
url: "http://url-to-license.com"
}
},
host: "localhost:8080",
basePath: "/admin-api/admin"
}

【问题讨论】:

    标签: java spring spring-mvc swagger swagger-ui


    【解决方案1】:

    解决了。在错过了很多事情后我找到了解决方案,我将方法 Docket api() 更改为

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any()).build().apiInfo(apiInfo());
    }
    

    我认为旧方法的PathSelectors.regex("/api/.*") 部分限制了端点的查找过程

    【讨论】:

      猜你喜欢
      • 2022-09-30
      • 2018-09-11
      • 2017-12-19
      • 2017-10-06
      • 2023-03-11
      • 1970-01-01
      • 2018-07-16
      • 2014-04-15
      • 2022-01-23
      相关资源
      最近更新 更多