简体   繁体   English

如何更改 Swagger-ui URL 前缀?

[英]How to change Swagger-ui URL prefix?

I am using Springfox Swagger2 with Spring boot 1.5.9.我将 Springfox Swagger2 与 Spring boot 1.5.9 一起使用。

I can access swagger UI on this link.我可以在此链接上访问 swagger UI。

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

How can I change it to be available on following URL?如何将其更改为在以下 URL 上可用?

http://localhost:8090/my/custom/path/swagger-ui.html 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();
}
}

Try this configuration class.试试这个配置类。

@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/");
  }


}

I've found a working solution for Springfox 3.0.0 here :我已经找到了Springfox 3.0.0工作解决方案在这里

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

Configuration above will change base-path for Swagger endpoints to /documentation without any redirects and other crutches.上面的配置会将 Swagger 端点的基本路径更改为/documentation而无需任何重定向和其他拐杖。

It is sad that these configurations is missing in the docs tho.遗憾的是,文档中缺少这些配置。

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

this works as per requirement.这按要求工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM