【问题标题】:How to change Swagger-ui URL prefix?如何更改 Swagger-ui URL 前缀?
【发布时间】:2018-11-15 16:10:55
【问题描述】:

我在 Spring boot 1.5.9 中使用 Springfox Swagger2。

我可以通过此链接访问 Swagger UI。

http://localhost:8090/swagger-ui.html

如何将其更改为在以下 URL 上可用?

http://localhost:8090/my/custom/path/swagger-ui.html

@EnableSwagger2
public class Configuration {

@Bean
public Docket api() { 
    return new Docket(DocumentationType.SWAGGER_2)  
      .select()
      .apis(RequestHandlerSelectors.basePackage("my.favorite.package"))
      .paths(PathSelectors.any())
      .build()
      .apiInfo(apiInfo()).useDefaultResponseMessages(false);
}

private ApiInfo apiInfo() {
    return new ApiInfoBuilder().title("My title").version("1.0")
            .contact(new Contact("Blah", "blah.com", "blah@blah.com")).build();
}
}

【问题讨论】:

标签: java spring-boot swagger swagger-ui swagger-2.0


【解决方案1】:

UPD:Springfox 已弃用

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

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

因此,如果您阅读本文,请考虑改用https://springdoc.org/

这是一个非常简单的转换,他们做得很好 记录它。 https://springdoc.org/#migrating-from-springfox.

原答案

我找到了适用于 Springfox 3.0.0 here 的有效解决方案:

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

上面的配置会将 Swagger 端点的基本路径更改为 /documentation,而无需任何重定向和其他拐杖。

很遗憾,文档中缺少这些配置。

【讨论】:

    【解决方案2】:

    您可以简单地使用 server.servlet.context-path=/my/custom/path/

    这按要求工作。

    【讨论】:

      【解决方案3】:

      试试这个配置类。

      @Configuration
      @EnableSwagger2
      public class SwaggerConfig extends WebMvcConfigurationSupport {
      
        @Bean
        public Docket productApi() {
          return new Docket(DocumentationType.SWAGGER_2)
              .select().apis(RequestHandlerSelectors.basePackage(""my.favorite.package""))
                              .paths(regex(PathSelectors.any()))
              .build();
      
        }
      
        @Override
        public void addViewControllers(ViewControllerRegistry registry) {
          registry.addRedirectViewController("/documentation/v2/api-docs", "/v2/api-docs").setKeepQueryParams(true);
          registry.addRedirectViewController("/documentation/swagger-resources/configuration/ui", "/swagger-resources/configuration/ui");
          registry.addRedirectViewController("/documentation/swagger-resources/configuration/security", "/swagger-resources/configuration/security");
          registry.addRedirectViewController("/documentation/swagger-resources", "/swagger-resources");
        }
      
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
          registry.addResourceHandler("/documentation/**").addResourceLocations("classpath:/META-INF/resources/");
        }
      
      
      }
      

      【讨论】:

      猜你喜欢
      • 2016-11-25
      • 2017-01-27
      • 2020-07-02
      • 1970-01-01
      • 1970-01-01
      • 2020-05-20
      • 1970-01-01
      • 2011-11-13
      • 2017-12-11
      相关资源
      最近更新 更多