Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does springfox-swagger2 UI tell me "Unable to infer base url."

Why does springfox-swagger2 UI tell me Unable to infer base url. As far as I know, I am using a typical Swagger spring-boot configuration.

As you can see in the screenshot, the swagger-fox url backing the UI is example.com/api . NOTE: I get a standard Spring Whitelabel Error Page when I navigate to: https://localhost:9600/api/v2/api-docs/ . I suspect this is the root of the problem? I see no errors that Spring didn't load springfox-swagger2 and so I don't know why that isn't working.

enter image description here

My config looks something like this (and I have tried all sorts of variations of this config, from searching the net for advice):

@EnableSwagger2 @EnableWebMvc @ComponentScan(basePackages = {"com.company.project"}) public class SwaggerConfig {     @Bean     public Docket api() {         return new Docket(DocumentationType.SWAGGER_2)                 .select()                 .apis(Predicates.not(RequestHandlerSelectors.basePackage("org.springframework.boot")))                 .apis(Predicates.not(RequestHandlerSelectors.basePackage("org.springframework.cloud")))                 .apis(Predicates.not(RequestHandlerSelectors.basePackage("org.springframework.data.rest.webmvc")))                 .paths(PathSelectors.any())                 .build();     } } 

And

<!-- to generate /swagger-ui.html --> <dependency>     <groupId>io.springfox</groupId>     <artifactId>springfox-swagger2</artifactId>     <version>2.7.0</version> </dependency>  <dependency>     <groupId>io.springfox</groupId>     <artifactId>springfox-swagger-ui</artifactId>     <version>2.7.0</version> </dependency> 

NOTE: Interestingly, when I try version 2.6.0, I don't get the modal popup but my Swagger UI shows 0 api content. So, i know that modal must be fairly new?

If there is not enough info here, leave me a comment.

like image 612
djangofan Avatar asked Nov 22 '17 00:11

djangofan


People also ask

What is the default URL for swagger UI?

By default, Swagger UI is accessible at /q/swagger-ui . The value / is not allowed as it blocks the application from serving anything else. A value prefixed with '/' makes it absolute and not relative. Once your application is started, you can go to http://localhost:8080/q/swagger-ui and play with your API.

How do I enable Swagger2 in spring boot?

To enable the Swagger2 in Spring Boot application, you need to add the following dependencies in our build configurations file. For Gradle users, add the following dependencies in your build. gradle file. Now, add the @EnableSwagger2 annotation in your main Spring Boot application.

How do I change my swagger UI URL in spring boot?

You can change default swagger-ui path programmatically using ApplicationListener<ApplicationPreparedEvent> . The idea is simple - override springdoc. swagger-ui. path=/custom/path before your Spring Boot application starts.


1 Answers

I was able to resolve the issue by adding SpringBootApplication with annotation - as suggested by Mark :

@EnableSwagger2 
like image 182
Shiva Achari Avatar answered Oct 16 '22 21:10

Shiva Achari