Reputation: 6804
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
Upvotes: 4
Views: 10634
Reputation: 1
Add Servlet initializer :-
public class ServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
}
OR
extends ServletInitializer
into your main applicationUpvotes: 0
Reputation: 189
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>
Upvotes: 0
Reputation: 1
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"**
Upvotes: 0