3

I am using Spring Boot 2.1.4.RELEASE with springfox-swagger2 2.9.2 and able to get data from microservices (if accessed with GET operation from browser directly)

Problem : When i hit Swaager UI on http://localhost:9000/swagger-ui.html to see the REST APIs documentation in detail , then i am getting error

Unable to infer base url. This is common when using dynamic servlet registration or when the API is behind an API Gateway. The base url is the root of where all the swagger resources are served. For e.g. if the api is available at http://example.org/api/v2/api-docs then the base url is http://example.org/api/. Please enter the location manually:

My Swagger Config is below

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class RestResourceConfig implements WebMvcConfigurer {

  @Override
  public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
    configurer.defaultContentType(MediaType.APPLICATION_JSON);
  }


  @Bean
  public Docket api() {
    return new Docket(DocumentationType.SWAGGER_2)
        .select()
        .apis(RequestHandlerSelectors.any())
        .paths(PathSelectors.any())
        .build();
  }
}

NOTE :

I can access api docs directly using link : http://localhost:9000/v2/api-docs There is no issue in that. Only problem with Swagger UI

Please recommend the hints/solution to fix

P.S - I am not using Spring Security

Arun Kumar
  • 6,534
  • 13
  • 40
  • 67
  • I had the problem serveral times too... In my case it suddenly worked after some clean ups and rebuilds, just like someone was still there from the last build and corrupted. I didn't make any changes. – Kikkirej Aug 15 '19 at 15:10
  • Are you using a configuration outside your SB project? As a common configuration? I've literally hit this and put @EnableSwagger2 in the Application class. I'm trying to figure out why I need to do that. – JRK Aug 15 '19 at 15:58
  • Does any of these existing answers help? [1](https://stackoverflow.com/q/47425048/113116), [2](https://stackoverflow.com/q/49155420/113116), [3](https://stackoverflow.com/q/54285556/113116), [4](https://stackoverflow.com/q/46520149/113116), [5](https://stackoverflow.com/q/56280202/113116) – Helen Aug 15 '19 at 16:57
  • @JRK : No, everything is inside same maven project. I am even using @ EnableAutoConfiguration in my main boot class to detect config classes automatically by Boot. – Arun Kumar Aug 16 '19 at 13:55
  • @Helen : Not much. Tried all solutions except 2 which is moreover related to spring security access – Arun Kumar Aug 16 '19 at 13:58

3 Answers3

0

I had the same problem as you.

My spring-boot version is 2.3.1 and swagger version is 2.10.5. These is my swagger dependencies and the third one helped me to fix a problem.

implementation "io.springfox:springfox-swagger2:2.10.5"
implementation "io.springfox:springfox-swagger-ui:2.10.5"
**implementation "io.springfox:springfox-data-rest:2.10.5"**
0

Maybe Help. I found that the Springboot Application Class need to add @EnableSwagger2 annotation.

IT'S BETTER TO USE SWAGGER BELOW 2.7.0

<dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>2.6.1</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>2.6.1</version>
    </dependency>
Baba Fakruddin
  • 139
  • 1
  • 4
0
  1. Add Servlet initializer :-

    public class ServletInitializer extends SpringBootServletInitializer {
    
        @Override
        protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
            return application.sources(Application.class);
        }
    }
    

OR

  1. Add extends ServletInitializer into your main application
DL1997
  • 1
  • 2