'Unable to infer base url... springfox-swagger2 version 2.9.2
Version of springfox-swagger that I am using [ ![Download] 1]2
What kind of issue is this?
Project structure:
SwaggerConfig is in automate.api.config.swagger.SwaggerConfiguration
Auth Config is in automate.api.web.auth.keycloak.FalconKeycloakConfigurerAdapter
dependencies
As for Spring Boot
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/>
</parent>
As for swagger
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${spring.springfox-swagger2.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${spring.springfox-swagger2.version}</version>
</dependency>
Swagger 2 config
@Configuration
@EnableSwagger2
public class SwaggerConfiguration {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
}
Security config
@Override
protected void configureAccessRules(final HttpSecurity http) throws Exception {
http.csrf()
.disable()
.authorizeRequests()
.antMatchers("/actuator/health")
.permitAll()
.antMatchers(HttpMethod.GET,
"/v2/api-docs",
"/swagger-resources/**",
"/swagger-ui.html**",
"/webjars/**",
"favicon.ico")
.permitAll();
}
Example for one of my controllers
All of my controllers mapped like /api/...
@RestController
@RequestMapping(value = "/api", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class ExampleResource {
private ExampleFacade catalog;
@Autowired
public ExampleResource(ExampleFacade catalog) {
this.catalog = catalog;
}
@RequestMapping(method = RequestMethod.GET, value = "/examples")
public List<ExampleDTO> index() {
return catalog.listExamples();
}
}
ISSUE
/v2/api-docs
works perfectly, I get back all of my endpoints with their details
/swagger-ui.html
gives me back a pop up that shows "Unable to infer base url......"
Then I turned on the TRACE log level on my spring-boot app, like
logging:
level:
org:
springframework:
web: TRACE
My logs looks like:
[2019-05-22 21:12:06] TRACE [nio-8080-exec-1] o.s.w.s.DispatcherServlet GET "/v2/api-docs", parameters={}, headers={masked} in DispatcherServlet 'dispatcherServlet'
[2019-05-22 21:12:06] TRACE [nio-8080-exec-1] o.s.w.s.m.m.a.ServletInvocableHandlerMethod Arguments: [null, SecurityContextHolderAwareRequestWrapper[ org.springframework.security.web.header.HeaderWriterFilter$HeaderWriterRequest@b9ed904]]
[2019-05-22 21:12:06] DEBUG [nio-8080-exec-1] o.s.w.s.m.m.a.HttpEntityMethodProcessor Using 'application/json;q=0.8', given [text/html, application/xhtml+xml, image/webp, image/apng, application/signed-exchange;v=b3, application/xml;q=0.9, */*;q=0.8] and supported [application/json, application/*+json, application/json, application/*+json]
[2019-05-22 21:12:06] TRACE [nio-8080-exec-1] o.s.w.s.m.m.a.HttpEntityMethodProcessor Writing [BaseView(data=springfox.documentation.spring.web.json.Json@65455d18)]
[2019-05-22 21:12:06] TRACE [nio-8080-exec-1] o.s.w.s.DispatcherServlet No view rendering, null ModelAndView returned.
[2019-05-22 21:12:06] DEBUG [nio-8080-exec-1] o.s.w.s.DispatcherServlet Completed 200 OK, headers={masked}
[2019-05-22 21:12:10] TRACE [nio-8080-exec-2] o.s.w.s.DispatcherServlet GET "/swagger-ui.html", parameters={}, headers={masked} in DispatcherServlet 'dispatcherServlet'
[2019-05-22 21:12:10] TRACE [nio-8080-exec-2] o.s.w.s.h.SimpleUrlHandlerMapping Mapped to HandlerExecutionChain with [ResourceHttpRequestHandler ["classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/", "/"]] and 5 interceptors
[2019-05-22 21:12:10] TRACE [nio-8080-exec-2] o.s.w.s.DispatcherServlet No view rendering, null ModelAndView returned.
[2019-05-22 21:12:10] DEBUG [nio-8080-exec-2] o.s.w.s.DispatcherServlet Completed 200 OK, headers={masked}
[2019-05-22 21:12:10] TRACE [nio-8080-exec-3] o.s.w.s.DispatcherServlet GET "/webjars/springfox-swagger-ui/springfox.css?v=2.9.2", parameters={masked}, headers={masked} in DispatcherServlet 'dispatcherServlet'
[2019-05-22 21:12:10] TRACE [nio-8080-exec-4] o.s.w.s.DispatcherServlet GET "/webjars/springfox-swagger-ui/swagger-ui.css?v=2.9.2", parameters={masked}, headers={masked} in DispatcherServlet 'dispatcherServlet'
[2019-05-22 21:12:10] TRACE [nio-8080-exec-3] o.s.w.s.h.SimpleUrlHandlerMapping Matching patterns [/webjars/**, /**]
[2019-05-22 21:12:10] TRACE [nio-8080-exec-3] o.s.w.s.h.SimpleUrlHandlerMapping Mapped to HandlerExecutionChain with [ResourceHttpRequestHandler ["classpath:/META-INF/resources/webjars/"]] and 5 interceptors
[2019-05-22 21:12:10] TRACE [nio-8080-exec-4] o.s.w.s.h.SimpleUrlHandlerMapping Matching patterns [/webjars/**, /**]
[2019-05-22 21:12:10] TRACE [nio-8080-exec-4] o.s.w.s.h.SimpleUrlHandlerMapping Mapped to HandlerExecutionChain with [ResourceHttpRequestHandler ["classpath:/META-INF/resources/webjars/"]] and 5 interceptors
[2019-05-22 21:12:10] TRACE [nio-8080-exec-6] o.s.w.s.DispatcherServlet GET "/webjars/springfox-swagger-ui/swagger-ui-bundle.js?v=2.9.2", parameters={masked}, headers={masked} in DispatcherServlet 'dispatcherServlet'
[2019-05-22 21:12:10] TRACE [nio-8080-exec-3] o.s.w.s.DispatcherServlet No view rendering, null ModelAndView returned.
[2019-05-22 21:12:10] DEBUG [nio-8080-exec-3] o.s.w.s.DispatcherServlet Completed 200 OK, headers={masked}
[2019-05-22 21:12:10] TRACE [nio-8080-exec-5] o.s.w.s.DispatcherServlet GET "/webjars/springfox-swagger-ui/swagger-ui-standalone-preset.js?v=2.9.2", parameters={masked}, headers={masked} in DispatcherServlet 'dispatcherServlet'
[2019-05-22 21:12:10] TRACE [nio-8080-exec-7] o.s.w.s.DispatcherServlet GET "/webjars/springfox-swagger-ui/springfox.js?v=2.9.2", parameters={masked}, headers={masked} in DispatcherServlet 'dispatcherServlet'
[2019-05-22 21:12:10] TRACE [nio-8080-exec-6] o.s.w.s.h.SimpleUrlHandlerMapping Matching patterns [/webjars/**, /**]
[2019-05-22 21:12:10] TRACE [nio-8080-exec-6] o.s.w.s.h.SimpleUrlHandlerMapping Mapped to HandlerExecutionChain with [ResourceHttpRequestHandler ["classpath:/META-INF/resources/webjars/"]] and 5 interceptors
[2019-05-22 21:12:10] TRACE [nio-8080-exec-5] o.s.w.s.h.SimpleUrlHandlerMapping Matching patterns [/webjars/**, /**]
[2019-05-22 21:12:10] TRACE [nio-8080-exec-5] o.s.w.s.h.SimpleUrlHandlerMapping Mapped to HandlerExecutionChain with [ResourceHttpRequestHandler ["classpath:/META-INF/resources/webjars/"]] and 5 interceptors
[2019-05-22 21:12:10] TRACE [nio-8080-exec-7] o.s.w.s.h.SimpleUrlHandlerMapping Matching patterns [/webjars/**, /**]
[2019-05-22 21:12:10] TRACE [nio-8080-exec-7] o.s.w.s.h.SimpleUrlHandlerMapping Mapped to HandlerExecutionChain with [ResourceHttpRequestHandler ["classpath:/META-INF/resources/webjars/"]] and 5 interceptors
[2019-05-22 21:12:10] TRACE [nio-8080-exec-4] o.s.w.s.DispatcherServlet No view rendering, null ModelAndView returned.
[2019-05-22 21:12:10] DEBUG [nio-8080-exec-4] o.s.w.s.DispatcherServlet Completed 200 OK, headers={masked}
[2019-05-22 21:12:10] TRACE [nio-8080-exec-7] o.s.w.s.DispatcherServlet No view rendering, null ModelAndView returned.
[2019-05-22 21:12:10] DEBUG [nio-8080-exec-7] o.s.w.s.DispatcherServlet Completed 200 OK, headers={masked}
[2019-05-22 21:12:10] TRACE [nio-8080-exec-5] o.s.w.s.DispatcherServlet No view rendering, null ModelAndView returned.
[2019-05-22 21:12:10] DEBUG [nio-8080-exec-5] o.s.w.s.DispatcherServlet Completed 200 OK, headers={masked}
[2019-05-22 21:12:10] TRACE [nio-8080-exec-6] o.s.w.s.DispatcherServlet No view rendering, null ModelAndView returned.
[2019-05-22 21:12:10] DEBUG [nio-8080-exec-6] o.s.w.s.DispatcherServlet Completed 200 OK, headers={masked}
[2019-05-22 21:12:10] TRACE [nio-8080-exec-8] o.s.w.s.DispatcherServlet GET "/swagger-resources/configuration/ui", parameters={}, headers={masked} in DispatcherServlet 'dispatcherServlet'
[2019-05-22 21:12:10] TRACE [nio-8080-exec-8] o.s.w.s.m.m.a.RequestMappingHandlerMapping Mapped to public org.springframework.http.ResponseEntity<springfox.documentation.swagger.web.UiConfiguration> springfox.documentation.swagger.web.ApiResourceController.uiConfiguration()
[2019-05-22 21:12:10] TRACE [nio-8080-exec-8] o.s.w.s.m.m.a.ServletInvocableHandlerMethod Arguments: []
[2019-05-22 21:12:10] DEBUG [nio-8080-exec-8] o.s.w.s.m.m.a.HttpEntityMethodProcessor Using 'application/json', given [application/json] and supported [application/json, application/*+json, application/json, application/*+json]
[2019-05-22 21:12:10] TRACE [nio-8080-exec-8] o.s.w.s.m.m.a.HttpEntityMethodProcessor Writing [BaseView(data=springfox.documentation.swagger.web.UiConfiguration@7594a0c7)]
[2019-05-22 21:12:10] TRACE [nio-8080-exec-8] o.s.w.s.DispatcherServlet No view rendering, null ModelAndView returned.
[2019-05-22 21:12:10] DEBUG [nio-8080-exec-8] o.s.w.s.DispatcherServlet Completed 200 OK, headers={masked}
[2019-05-22 21:12:10] TRACE [nio-8080-exec-9] o.s.w.s.DispatcherServlet GET "/swagger-resources/configuration/security", parameters={}, headers={masked} in DispatcherServlet 'dispatcherServlet'
[2019-05-22 21:12:10] TRACE [nio-8080-exec-9] o.s.w.s.m.m.a.RequestMappingHandlerMapping Mapped to public org.springframework.http.ResponseEntity<springfox.documentation.swagger.web.SecurityConfiguration> springfox.documentation.swagger.web.ApiResourceController.securityConfiguration()
[2019-05-22 21:12:10] TRACE [nio-8080-exec-9] o.s.w.s.m.m.a.ServletInvocableHandlerMethod Arguments: []
[2019-05-22 21:12:10] DEBUG [nio-8080-exec-9] o.s.w.s.m.m.a.HttpEntityMethodProcessor Using 'application/json', given [application/json] and supported [application/json, application/*+json, application/json, application/*+json]
[2019-05-22 21:12:10] TRACE [nio-8080-exec-9] o.s.w.s.m.m.a.HttpEntityMethodProcessor Writing [BaseView(data=springfox.documentation.swagger.web.SecurityConfiguration@4605258b)]
[2019-05-22 21:12:10] TRACE [nio-8080-exec-9] o.s.w.s.DispatcherServlet No view rendering, null ModelAndView returned.
[2019-05-22 21:12:10] DEBUG [nio-8080-exec-9] o.s.w.s.DispatcherServlet Completed 200 OK, headers={masked}
[2019-05-22 21:12:10] TRACE [io-8080-exec-10] o.s.w.s.DispatcherServlet GET "/swagger-resources", parameters={}, headers={masked} in DispatcherServlet 'dispatcherServlet'
[2019-05-22 21:12:10] TRACE [io-8080-exec-10] o.s.w.s.m.m.a.RequestMappingHandlerMapping Mapped to public org.springframework.http.ResponseEntity<java.util.List<springfox.documentation.swagger.web.SwaggerResource>> springfox.documentation.swagger.web.ApiResourceController.swaggerResources()
[2019-05-22 21:12:10] TRACE [io-8080-exec-10] o.s.w.s.m.m.a.ServletInvocableHandlerMethod Arguments: []
[2019-05-22 21:12:10] DEBUG [io-8080-exec-10] o.s.w.s.m.m.a.HttpEntityMethodProcessor Using 'application/json', given [application/json] and supported [application/json, application/*+json, application/json, application/*+json]
[2019-05-22 21:12:10] TRACE [io-8080-exec-10] o.s.w.s.m.m.a.HttpEntityMethodProcessor Writing [BaseView(data=[springfox.documentation.swagger.web.SwaggerResource@b35c0d2])]
[2019-05-22 21:12:10] TRACE [io-8080-exec-10] o.s.w.s.DispatcherServlet No view rendering, null ModelAndView returned.
[2019-05-22 21:12:10] DEBUG [io-8080-exec-10] o.s.w.s.DispatcherServlet Completed 200 OK, headers={masked}
[2019-05-22 21:12:11] TRACE [nio-8080-exec-1] o.s.w.s.DispatcherServlet GET "/null/swagger-resources/configuration/ui", parameters={}, headers={masked} in DispatcherServlet 'dispatcherServlet'
[2019-05-22 21:12:11] TRACE [nio-8080-exec-1] o.s.w.s.h.SimpleUrlHandlerMapping Mapped to HandlerExecutionChain with [ResourceHttpRequestHandler ["classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/", "/"]] and 5 interceptors
[2019-05-22 21:12:11] DEBUG [nio-8080-exec-1] o.s.w.s.r.ResourceHttpRequestHandler Resource not found
[2019-05-22 21:12:11] TRACE [nio-8080-exec-1] o.s.w.s.DispatcherServlet No view rendering, null ModelAndView returned.
[2019-05-22 21:12:11] DEBUG [nio-8080-exec-1] o.s.w.s.DispatcherServlet Completed 404 NOT_FOUND, headers={masked}
[2019-05-22 21:12:11] TRACE [nio-8080-exec-1] o.s.w.s.DispatcherServlet "ERROR" dispatch for GET "/error", parameters={}, headers={masked} in DispatcherServlet 'dispatcherServlet'
[2019-05-22 21:12:11] TRACE [nio-8080-exec-1] o.s.w.s.m.m.a.RequestMappingHandlerMapping Mapped to public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
[2019-05-22 21:12:11] TRACE [nio-8080-exec-1] o.s.w.s.m.m.a.ServletInvocableHandlerMethod Arguments: [SecurityContextHolderAwareRequestWrapper[ FirewalledRequest[ org.apache.catalina.core.ApplicationHttpRequest@dc7df80]]]
[2019-05-22 21:12:11] DEBUG [nio-8080-exec-1] o.s.w.s.m.m.a.HttpEntityMethodProcessor Using 'application/json', given [application/json] and supported [application/json, application/*+json, application/json, application/*+json]
[2019-05-22 21:12:11] TRACE [nio-8080-exec-1] o.s.w.s.m.m.a.HttpEntityMethodProcessor Writing [BaseView(data={timestamp=Wed May 22 21:12:11 CEST 2019, status=404, error=Not Found, message=No message available, path=/null/swagger-resources/configuration/ui})]
[2019-05-22 21:12:11] TRACE [nio-8080-exec-1] o.s.w.s.DispatcherServlet No view rendering, null ModelAndView returned.
[2019-05-22 21:12:11] DEBUG [nio-8080-exec-1] o.s.w.s.DispatcherServlet Exiting from "ERROR" dispatch, status 404, headers={masked}
[2019-05-22 21:12:12] TRACE [nio-8080-exec-2] o.s.w.s.DispatcherServlet GET "/null/swagger-resources/configuration/security", parameters={}, headers={masked} in DispatcherServlet 'dispatcherServlet'
[2019-05-22 21:12:12] TRACE [nio-8080-exec-2] o.s.w.s.h.SimpleUrlHandlerMapping Mapped to HandlerExecutionChain with [ResourceHttpRequestHandler ["classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/", "/"]] and 5 interceptors
[2019-05-22 21:12:12] DEBUG [nio-8080-exec-2] o.s.w.s.r.ResourceHttpRequestHandler Resource not found
[2019-05-22 21:12:12] TRACE [nio-8080-exec-2] o.s.w.s.DispatcherServlet No view rendering, null ModelAndView returned.
[2019-05-22 21:12:12] DEBUG [nio-8080-exec-2] o.s.w.s.DispatcherServlet Completed 404 NOT_FOUND, headers={masked}
[2019-05-22 21:12:12] TRACE [nio-8080-exec-2] o.s.w.s.DispatcherServlet "ERROR" dispatch for GET "/error", parameters={}, headers={masked} in DispatcherServlet 'dispatcherServlet'
[2019-05-22 21:12:12] TRACE [nio-8080-exec-2] o.s.w.s.m.m.a.RequestMappingHandlerMapping Mapped to public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
[2019-05-22 21:12:12] TRACE [nio-8080-exec-2] o.s.w.s.m.m.a.ServletInvocableHandlerMethod Arguments: [SecurityContextHolderAwareRequestWrapper[ FirewalledRequest[ org.apache.catalina.core.ApplicationHttpRequest@723fa0fe]]]
[2019-05-22 21:12:12] DEBUG [nio-8080-exec-2] o.s.w.s.m.m.a.HttpEntityMethodProcessor Using 'application/json', given [application/json] and supported [application/json, application/*+json, application/json, application/*+json]
[2019-05-22 21:12:12] TRACE [nio-8080-exec-2] o.s.w.s.m.m.a.HttpEntityMethodProcessor Writing [BaseView(data={timestamp=Wed May 22 21:12:12 CEST 2019, status=404, error=Not Found, message=No message available, path=/null/swagger-resources/configuration/security})]
[2019-05-22 21:12:12] TRACE [nio-8080-exec-2] o.s.w.s.DispatcherServlet No view rendering, null ModelAndView returned.
[2019-05-22 21:12:12] DEBUG [nio-8080-exec-2] o.s.w.s.DispatcherServlet Exiting from "ERROR" dispatch, status 404, headers={masked}
Unfortunately, I see there is something weird with the path mapping at the and of the logs: GET "/null/swagger-resources/configuration/ui" GET "/null/swagger-resources/configuration/security" etc...
Please, help me ! :)
I gooogled for this problem and read these ones:
Why does springfox-swagger2 UI tell me "Unable to infer base url."
https://github.com/springfox/springfox/issues/2191
https://github.com/springfox/springfox/issues/2907
swagger-ui.html 400 bad request
https://github.com/springfox/springfox/issues/2501 https://github.com/springfox/springfox/issues/2396
Solution 1:[1]
Now, I managed to solve my issue. One of the issues was the pathMApping.
I needed to change my configuration to explicitly to "/", like here
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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 SwaggerConfiguration {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build()
.pathMapping("/");
}
}
Other issue was I used ResponseBodyAdvice
import io.falcon.automate.api.web.views.BaseView;
import io.falcon.automate.api.web.views.ErrorView;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
@ControllerAdvice
public class ResponseAdvice implements ResponseBodyAdvice<Object> {
@Override
public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
return true;
}
/**
* Wraps all responses in base view class
* @param body
* @param returnType
* @param selectedContentType
* @param selectedConverterType
* @param request
* @param response
* @return
*/
@Override
public Object beforeBodyWrite(
Object body,
MethodParameter returnType,
MediaType selectedContentType,
Class<? extends HttpMessageConverter<?>> selectedConverterType,
ServerHttpRequest request,
ServerHttpResponse response
) {
if (body instanceof ErrorView) {
return body;
}
return new BaseView(body);
}
}
If I commented out my response wrapper class, everything works like a charm.....
The only issue, that I need my response wrapper. :/
Solution 2:[2]
To solve the issue you just need to add the following to the ResponseBodyAdvice implemented class, at @ControllerAdvice annotation you just need to define the base package of your application directory, so this won't return a null when calling the swagger-ui url.
For example, have a look at first line @ControllerAdvice annotation input parameter
@ControllerAdvice("com.main.abc.package")
public class AdapterAdvice implements ResponseBodyAdvice<Object> {
@Override
public boolean supports(MethodParameter methodParameter, Class<? extends HttpMessageConverter<?>> aClass) {
return true;
}
@Override
public Object beforeBodyWrite(
Object body,
MethodParameter methodParameter,
MediaType mediaType,
Class<? extends HttpMessageConverter<?>> aClass,
ServerHttpRequest serverHttpRequest,
ServerHttpResponse serverHttpResponse) {
// just some wrapper logic, you can just omit the below and return the body Object you got as a param
Map<String, Object> data = new HashMap<>();
data.put("serverTime", new Date(System.currentTimeMillis()));
if(body instanceof Map && ((Map)body).get("error") != null ){
data.put("isSuccess", false);
if(((Map)body).get("trace") != null){
((Map)body).remove("trace");
}
} else if (body instanceof ApiError && ((ApiError)body).getResponseStatusCode().equalsIgnoreCase("0")){
data.put("isSuccess", false);
} else {
data.put("isSuccess", true);
}
data.put("mainResponse",body);
return data;
}
}
Solution 3:[3]
In my case with Swagger 3.0.0 - especially io.springfox:springfox-boot-starter - specifying the spring-boot URL context-path solved this loop with "/null/swagger-resources/":
In application.properties: server.servlet.context-path=/
Or if you have an application.yml file:
server:
servlet:
context-path: /
Warning: It will change the base path of all web services, not only Swagger
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | lombocska |
Solution 2 | |
Solution 3 | electrobabe |