java - 如何为 swagger 2.8.0 做友好的基本 url

标签 java spring-boot rest swagger springfox

我正在尝试更改 API 文档的基本访问 URL。网址是“http://localhost:8080/swagger-ui.html”。我想得到类似“http://localhost:8080/myapi/swagger-ui.html ”的东西。

我使用 Springfox 2.8.0 Swagger、Java 8、Spring Boot 2.0 Swagger 的配置是:

@Configuration
@EnableSwagger2
public class SwaggerConfiguration {

    @Bean
    public Docket api(ServletContext servletContext) {
        return new Docket(DocumentationType.SWAGGER_2)
                .pathProvider(new RelativePathProvider(servletContext) {
                    @Override
                    public String getApplicationBasePath() {
                        return "/myapi";
                    }
                })
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(Predicates.not(PathSelectors.regex("/error")))
                .build()
                .useDefaultResponseMessages(false);
    }
}

自定义路径提供程序必须提供帮助,但我仍然可以通过使用 url“http://localhost:8080/swagger-ui.html”来访问 api 文档。如果我使用 url“http://localhost:8080/myapi/swagger-ui.html”,我会收到 404 错误。请看下面的屏幕截图。

enter image description here

最佳答案

UPD:Springfox 被遗弃

Springfox Swagger 一直是一个有点脏的解决方案,有很多不清楚和错误,但到现在(2021 年第四季度)它已经一年多没有更新了。

最后一根稻草是 Springfox Swagger 3.0 doesn't work anymore with Spring Boot 2.6.x.

因此,如果您正在阅读本文,请考虑切换到 https://springdoc.org/相反。

It's a pretty straightforward conversion and they do a great job of documenting it. https://springdoc.org/#migrating-from-springfox.

对于那些使用 Springfox Swagger 3.0.0 的人

Here's更改文档基本 url 的工作配置:

springfox:
  documentation:
    swaggerUi:
      baseUrl: /documentation
    openApi:
      v3:
        path: /documentation/v3/api-docs
    swagger:
      v2:
        path: /documentation/v2/api-docs

关于java - 如何为 swagger 2.8.0 做友好的基本 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49924266/

相关文章:

java - 当 Android fragment onCallBack 带有数据时出现 NullPointerException

Java for循环跳过运行两次

java - Spring 状态机错误处理不起作用

Java 编译正则表达式错误

java - Java XMLRPC 中的 Python 元组

java - 如何使用spring boot和spring data配置两个实例mongodb

spring-boot - JSONDoc 未检测到多模块项目中模型的 @ApiObject 注释

javascript - ElasticSearch Access-Control-Allow-Headers header 不存在

java - Google 图片搜索 API 最大结果大小

android - 使用 Retrofit 调用 api