spring-boot - Swagger-UI 在根端点上不显示 SpringBoot 和路径参数

标签 spring-boot swagger

我正在尝试为现有的 Spring Boot API 项目设置 swagger。我发现我有两个端点导致 http://url/swagger-ui.html 文件无法呈现。如果我注释掉这两个端点,它就会正确呈现。

当两个端点存在时,http://url/v2/api-docs json 文件确实呈现成功。我可以从 api-docs 中获取 json 并将其粘贴到 https://editor.swagger.io/ 中,html 页面在该站点上正确呈现。

导致问题的两个端点位于根路径上,并且在 url 中只有一个路径参数。它们每个都在一个 Controller 中,该 Controller 具有 @RequestMapping("/") 并且它们被注释;

@PutMapping(value = "{vaultTitleId}", produces = MediaType.APPLICATION_JSON_VALUE)

@DeleteMapping(value = "{vaultTitleId}")

这两个端点工作正常,它们只是导致 Swagger 呈现 HTML 时出现一些问题。如果我删除它们,则会显示 HTML。我已经尝试将它们自己移动到 Controller 中,看看我是否可以防止 swagger 在 Swagger 配置中访问它们。但是,它们似乎只需要存在于 spring boot 可以看到它们的地方,以防止 html 显示。

欢迎任何建议。我想使用 Swagger,但我现在放弃了,正在寻找替代工具。

Gradle

compile("io.springfox:springfox-swagger2:2.9.2")
compile("io.springfox:springfox-swagger-ui:2.9.2")

Swagger 配置

@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
    return new Docket(DocumentationType.SWAGGER_2)
            .select()
            .apis(RequestHandlerSelectors
            .basePackage("com.nextgearcapital.mt.controller"))
            .paths(PathSelectors.any())
            .build();
}
}

最佳答案

我发现了一个解决方法。这是在另一个论坛中提出的。

https://github.com/springfox/springfox/issues/631

The problem is you're endpoints are consuming the endpoint mappings, you can create a regular expression that resolves it?

@DeleteMapping(value = "{vaultTitleId:^((?!swagger-ui.html).)*$}") Seems al lil clunky but not sure what the right answer is.

使用带有正则表达式的请求映射来定义路径参数确实允许显示 swagger-ui。我不清楚为什么会这样。我认为它更像是一种解决方法,而不是实际的解决方案。但是,这就是我要使用的。

    @DeleteMapping(value = "{vaultTitleId:^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$}", produces = MediaType.APPLICATION_JSON_VALUE)

关于spring-boot - Swagger-UI 在根端点上不显示 SpringBoot 和路径参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51635961/

相关文章:

java - Spring JPA 数据 : Custom Generic Repositories and Services: UnsatisfiedDependencyException

java - @Mock 导致异常,但 @MockBean 在 Spring Boot 应用程序中工作

java - spring boot 找不到资源文件夹下的 index.html

java - 如何防止Camel swagger 2.16.2的枚举参数类型

java - 为什么 Swagger + RESTEasy 不能使用 @ApplicationPath ("")

java - hibernate 注释问题

java - 从配置服务器获取客户端端口

swagger - 在 OpenAPI/Swagger 文件中声明 'char' 的正确方法是什么?

java - 带有 Apache Camel 3 (servlet) + SpringBoot 2 的 Swagger UI

swagger - 使用 swagger 4.x 包生成 swagger 2.0 yaml