How to change Swagger-ui URL prefix?
Asked Answered
F

3

11

I am using Springfox Swagger2 with Spring boot 1.5.9.

I can access swagger UI on this link.

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

How can I change it to be available on following 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", "[email protected]")).build();
}
}
Fencesitter answered 6/6, 2018 at 4:58 Comment(2)
Please refer: github.com/springfox/springfox/issues/977Ophthalmologist
This solution works perfectly for me https://stackoverflow.com/a/59948001Pict
B
10

UPD: Springfox is abandoned

Springfox Swagger had always been kinda dirty solution with a lot of unclearness and bugs, but by now (2021 Q4) it hadn't been updated for more than a year.

The final straw was the fact that Springfox Swagger 3.0 doesn't work anymore with Spring Boot 2.6.x.

So, if you reading this, please, consider switching over to https://springdoc.org/ instead.

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

Original answer

I've found a working solution for Springfox 3.0.0 here:

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.

It is sad that these configurations is missing in the docs tho.

Brezhnev answered 15/3, 2021 at 14:59 Comment(2)
Springfox 3.0 seems to work with 2.7.4 ok. Not suggesting you shouldn't migrate to Springdoc,Maytime
What should we do in case of Springdoc?Kuster
T
6

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


}
Townley answered 6/6, 2018 at 5:49 Comment(4)
I added above configuration but it made no difference. Swagger is still available on localhost:8090/swagger-ui.html and new URL localhost:8090/custom-path/swagger-ui.html does not work.Fencesitter
@RaviGupta try this one of the examples please, programcreek.com/java-api-examples/…Townley
I edited my solution besides you can find a huge conversation about this issue in here. github.com/springfox/springfox/issues/1080 @RaviGuptaTownley
Thanks... this worked for me however I needed to add the followings too: registry.addResourceHandler("swagger-ui.html") .addResourceLocations("classpath:/META-INF/resources/") and registry.addResourceHandler("/webjars/**") .addResourceLocations("classpath:/META-INF/resources/webjars/")Verine
W
1

You can simply use server.servlet.context-path=/my/custom/path/

this works as per requirement.

Weaponry answered 30/8, 2021 at 8:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.