Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change basePath for Springfox Swagger 2.0

I'm running a service, where Swagger UI is accessible at:

http://serviceURL/swagger-ui.html 

However, it is behind a proxy, such as:

http://proxyURL/serviceName 

Generated URLs by Swagger UI are looking like:

http://proxyURL/ 

instead of the actual URL with the serviceName as suffix. As far as I get it, this means manipulating the basePath property. As per documentation:

A swagger API documentation can no longer describe operations on different base paths. In 1.2 and earlier, each resource could have had a separate basePath. In 2.0, the basePath equivalents (schemes+host+basePath) are defined for the whole specification.

@Api(basePath) is deprecated, and it doesn't say what to use and how to use it. How to make the paths generated by Swagger appear properly?

I'm using Spring Boot, Springfox Swagger and annotations.

like image 541
Martin Asenov Avatar asked Mar 24 '16 11:03

Martin Asenov


People also ask

How do you set the base path in Swagger?

In OpenAPI 3.0, you can use an array of server elements to specify one or more base URLs for your API. You can register the servers using the annotation @Server: in @OpenAPIDefinition (for all operations) in @Operation (for a single operation)

How do I update my Swagger document?

Click the REST API definition that you want to work with. and then click Update. Click Choose File or Browse depending on which is displayed and, in your file system, select the OpenAPI (Swagger 2.0) definition you want to use. Click Update.

What is the default Swagger URL?

Add and configure Swagger middleware The generated document describing the endpoints appears as shown in OpenAPI specification (openapi. json). The Swagger UI can be found at https://localhost:<port>/swagger .


2 Answers

@Bean public Docket newsApi(ServletContext servletContext) {     return new Docket(DocumentationType.SWAGGER_2).pathProvider(new RelativePathProvider(servletContext) {         @Override         public String getApplicationBasePath() {             return "/serviceName" + super.getApplicationBasePath();         }     }).host("proxyURL"); } 
like image 93
astafev.evgeny Avatar answered Sep 19 '22 01:09

astafev.evgeny


You can edit your SwaggerConfiguration like that:

Take care to replace the package (which need to be the one containing your REST controllers), the host, and the PATH you need

@Configuration @EnableSwagger2 public class SwaggerConfiguration implements WebMvcConfigurer {      public static final String PATH = "/serviceName";      @Bean     public Docket api() {         final var package = "com.martin.rest";         final var host = "localhost:8080";          return new Docket(DocumentationType.SWAGGER_2)                 .host(host)                 .select()                 .apis(RequestHandlerSelectors.basePackage(package))                 .paths(PathSelectors.any())                 .build();     }      @Override     public void addViewControllers(ViewControllerRegistry registry) {         final var apiDocs = "/v2/api-docs";         final var configUi = "/swagger-resources/configuration/ui";         final var configSecurity = "/swagger-resources/configuration/security";         final var resources = "/swagger-resources";          registry.addRedirectViewController(PATH + apiDocs, apiDocs).setKeepQueryParams(true);         registry.addRedirectViewController(PATH + resources, resources);         registry.addRedirectViewController(PATH + configUi, configUi);         registry.addRedirectViewController(PATH + configSecurity, configSecurity);         registry.addRedirectViewController(PATH, "/");     }      @Override     public void addResourceHandlers(ResourceHandlerRegistry registry) {         registry.addResourceHandler(PATH + "/**").addResourceLocations("classpath:/META-INF/resources/");     } } 

Another solution is by changing the spring-boot URL context-path:

Edit pour application.properties file:

server.servlet.context-path=/serviceName 

Or if you have an application.yml file:

server:   servlet:     context-path: /serviceName 

Warning: It will change the base path of all your web services, not only Swagger

like image 21
veben Avatar answered Sep 19 '22 01:09

veben