【问题标题】:Swagger showing not existing endpoints. How to get rid of them?大摇大摆地显示不存在的端点。如何摆脱它们?
【发布时间】:2017-07-17 10:17:24
【问题描述】:

我正在 Spring Boot 中构建一个非常简单的 REST 服务,请求如下:

  • GET /api/resources
  • POST /api/resources
  • GET /api/resources/id
  • 删除 /api/resources/id

但是当我转到 localhost:8080/swagger-ui.html 时,我会得到一个非常长的列表,其中包含不存在的冗余端点,例如:

  • 删除 /api/resources
  • 补丁/api/resources
  • HEAD /api/resources
  • 选项/api/resources
  • 补丁/api/resources/id
  • HEAD /api/resources/id
  • 选项/api/resources/id

那么,如何摆脱它们?我一直在寻找答案,但我只发现了如何将响应列表限制为特定路径,这不是我的问题。

我无法通过注释 @ApiOperation(value = "xyz", hidden = true) 隐藏它们,因为这些请求在我的控制器代码中不存在。

这是我的 SwaggerConfig.java 类:

@Configuration
@EnableSwagger2
class SwaggerConfig {

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

顺便说一句,显然我在 /v2/api-docs 上收到 404 错误,但我认为这不是我的问题,因为 Swagger 显示了正确的端点列表,但也显示了许多不存在的端点。虽然我还没有找到这个 404 错误的解决方案,但我不知道我是否应该关心。

【问题讨论】:

    标签: java spring-boot swagger


    【解决方案1】:

    原来,我的控制器代码是问题所在:

     //@RequestMapping("/resource/{id}")
     @RequestMapping(value = "/resource/{id}", method = RequestMethod.GET)
    

    必须在任何地方指定 @RequestMapping 中的方法才能在 Swagger 中获取正确的端点列表,即使 REST 服务有时也可以正常工作而无需指定这些方法。

    【讨论】:

      【解决方案2】:

      是的,或者您使用@GetMapping(显然用于GET 方法)或@PostMapping 用于POST,并且您不需要指定方法。 https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/bind/annotation/GetMapping.html

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-02-23
        • 1970-01-01
        • 1970-01-01
        • 2019-05-11
        • 1970-01-01
        • 2022-09-28
        • 1970-01-01
        • 2018-11-29
        相关资源
        最近更新 更多