Unable to infer base url... springfox-swagger2 version 2.9.2 - spring-boot

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

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. :/

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;
}
}

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

Related

Unable to fetch the view file

On making a GET Request which returns a ModelAndView Object I am getting the following error
: GET "/tweet2?email=tim#gmail.com", parameters={masked} 2022-03-08
11:04:45.459 DEBUG 46576 --- [nio-8080-exec-3]
s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to
com.example.demo.RestAPIExample#getTweetsByEmail(String) 2022-03-08
11:04:45.464 DEBUG 46576 --- [nio-8080-exec-3]
o.s.w.s.v.ContentNegotiatingViewResolver : Selected '/' given [/]
2022-03-08 11:04:45.464 DEBUG 46576 --- [nio-8080-exec-3]
o.s.w.servlet.view.InternalResourceView : View name 'tweets', model
{tweets=[com.example.demo.Tweet#3a7a85cb]} 2022-03-08 11:04:45.465
DEBUG 46576 --- [nio-8080-exec-3]
o.s.w.servlet.view.InternalResourceView : Forwarding to [tweets]
2022-03-08 11:04:45.467 DEBUG 46576 --- [nio-8080-exec-3]
o.s.web.servlet.DispatcherServlet : "FORWARD" dispatch for GET
"/tweets?email=tim#gmail.com", parameters={masked} 2022-03-08
11:04:45.470 DEBUG 46576 --- [nio-8080-exec-3]
o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped to
ResourceHttpRequestHandler [classpath [META-INF/resources/], classpath
[resources/], classpath [static/], classpath [public/], ServletContext
[/]] 2022-03-08 11:04:45.472 DEBUG 46576 --- [nio-8080-exec-3]
o.s.w.s.r.ResourceHttpRequestHandler : Resource not found
2022-03-08 11:04:45.473 DEBUG 46576 --- [nio-8080-exec-3]
o.s.web.servlet.DispatcherServlet : Exiting from "FORWARD"
dispatch, status 404 2022-03-08 11:04:45.473 DEBUG 46576 ---
[nio-8080-exec-3] o.s.web.servlet.DispatcherServlet : Completed
404 NOT_FOUND 2022-03-08 11:04:45.474 DEBUG 46576 ---
[nio-8080-exec-3] o.s.web.servlet.DispatcherServlet : "ERROR"
dispatch for GET "/error?email=tim#gmail.com", parameters={masked}
2022-03-08 11:04:45.475 DEBUG 46576 --- [nio-8080-exec-3]
s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to
org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController#error(HttpServletRequest)
2022-03-08 11:04:45.482 DEBUG 46576 --- [nio-8080-exec-3]
o.s.w.s.m.m.a.HttpEntityMethodProcessor : Using 'application/json',
given [/] and supported [application/json, application/+json,
application/json, application/+json] 2022-03-08 11:04:45.483 DEBUG
46576 --- [nio-8080-exec-3] o.s.w.s.m.m.a.HttpEntityMethodProcessor :
Writing [{timestamp=Tue Mar 08 11:04:45 IST 2022, status=404,
error=Not Found, path=/tweet2}] 2022-03-08 11:04:45.497 DEBUG 46576
--- [nio-8080-exec-3] o.s.web.servlet.DispatcherServlet : Exiting from "ERROR" dispatch, status 404
Here is the code I wrote:
#GetMapping("/tweet2")
public ModelAndView getTweetsByEmail(#RequestParam String email) {
ModelAndView modelAndView = new ModelAndView("tweets");
List<Tweet> tweets = tweetMap.get(email);
modelAndView.getModel().put("tweets",tweets);
return modelAndView;
}
And there is a tweets.mustache file under the resources folder. Not sure why its unable to detect it

Wrong Header of the API versioning of the Post Request does not come to handleNoHandlerFoundException?

I am using Spring Boot v2.1.7 + HATEOAS + Spring Rest + Spring Security. When consumer doesn't pass the correct Custom Header in the request, say passes X-Accept-Version=v5, it gives me below error.
Error:
2020-03-26 15:44:48.201 DEBUG [employee-service,14c23adbe2664530,14c23adbe2664530,false] 3608 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : POST "/employee-catalog-api/reference-types", parameters={}
2020-03-26 15:44:48.216 DEBUG [employee-service,14c23adbe2664530,14c23adbe2664530,false] 3608 --- [nio-8080-exec-1] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped to ResourceHttpRequestHandler ["classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/", "/"]
2020-03-26 15:44:48.217 DEBUG [employee-service,14c23adbe2664530,14c23adbe2664530,false] 3608 --- [nio-8080-exec-1] .m.c.d.m.p.s.SAMLUserIdentityServiceImpl : Trying to get UserId from Security Context
2020-03-26 15:44:48.224 DEBUG [employee-service,14c23adbe2664530,14c23adbe2664530,false] 3608 --- [nio-8080-exec-1] o.j.s.OpenEntityManagerInViewInterceptor : Opening JPA EntityManager in OpenEntityManagerInViewInterceptor
2020-03-26 15:44:48.234 DEBUG [employee-service,14c23adbe2664530,14c23adbe2664530,false] 3608 --- [nio-8080-exec-1] o.s.w.s.r.ResourceHttpRequestHandler : Resource not found
2020-03-26 15:44:48.234 DEBUG [employee-service,14c23adbe2664530,14c23adbe2664530,false] 3608 --- [nio-8080-exec-1] o.s.s.w.header.writers.HstsHeaderWriter : Not injecting HSTS header since it did not match the requestMatcher org.springframework.security.web.header.writers.HstsHeaderWriter$SecureRequestMatcher#5c85f23b
2020-03-26 15:44:48.234 DEBUG [employee-service,14c23adbe2664530,14c23adbe2664530,false] 3608 --- [nio-8080-exec-1] w.c.HttpSessionSecurityContextRepository : SecurityContext is empty or contents are anonymous - context will not be stored in HttpSession.
2020-03-26 15:44:48.254 DEBUG [employee-service,14c23adbe2664530,14c23adbe2664530,false] 3608 --- [nio-8080-exec-1] o.j.s.OpenEntityManagerInViewInterceptor : Closing JPA EntityManager in OpenEntityManagerInViewInterceptor
2020-03-26 15:44:48.254 DEBUG [employee-service,14c23adbe2664530,14c23adbe2664530,false] 3608 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed 404 NOT_FOUND
2020-03-26 15:44:48.258 DEBUG [employee-service,14c23adbe2664530,14c23adbe2664530,false] 3608 --- [nio-8080-exec-1] o.s.s.w.a.ExceptionTranslationFilter : Chain processed normally
2020-03-26 15:44:48.258 DEBUG [employee-service,14c23adbe2664530,14c23adbe2664530,false] 3608 --- [nio-8080-exec-1] s.s.w.c.SecurityContextPersistenceFilter : SecurityContextHolder now cleared, as request processing completed
Code:
#PostMapping(path = "/employee-types", headers = {X-Accept-Version=v1})
public ResponseEntity<Integer> saveEmployeeType(#Valid #RequestBody EmployeeDto employeeDto) {
.....
......
......
return new ResponseEntity<>(HttpStatus.OK);
}
Why its not coming to handleNoHandlerFoundException of the #ControllerAdvice ?
#Override
protected ResponseEntity<Object> handleNoHandlerFoundException(NoHandlerFoundException ex, HttpHeaders headers,
HttpStatus status, WebRequest request) {
...................
return handleExceptionInternal(ex, error, getHeaders(), HttpStatus.BAD_REQUEST, request);
}
I was able to solve this issue by taking a reference from : How to set default value of exported as false in rest resource spring data rest.
By adding below logic, it works greatly.
#Component
public class SpringRestConfiguration extends RepositoryRestConfigurerAdapter {
#Override
public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
config.setRepositoryDetectionStrategy(RepositoryDetectionStrategy.RepositoryDetectionStrategies.ANNOTATED);
config.setExposeRepositoryMethodsByDefault(false);
}
}

Swagger2 ui not accessbile

I am using Swagger in a Spring boot application,
I somehow can access most of Swagger's endpoints such as /v2/api-docs, /swagger-resources but I can't figure out why /swagger-ui.html is not accessible.
I am using these dependencies:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
</dependency>
here is my Swagger Config class:
#Configuration
#EnableSwagger2
public class SwaggerConfig {
#Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("app.controllers"))
.paths(PathSelectors.any())
.build();
}
}
Here is the interesting part of the log:
2017-12-27 14:12:09.896 DEBUG 10212 --- [nio-8080-exec-1] o.s.security.web.FilterChainProxy : /springfox/swagger-ui.html at position 12 of 13 in additional filter chain; firing Filter: 'ExceptionTranslationFilter'
2017-12-27 14:12:09.896 DEBUG 10212 --- [nio-8080-exec-1] o.s.security.web.FilterChainProxy : /springfox/swagger-ui.html at position 13 of 13 in additional filter chain; firing Filter: 'FilterSecurityInterceptor'
2017-12-27 14:12:09.897 DEBUG 10212 --- [nio-8080-exec-1] o.s.s.w.u.matcher.AntPathRequestMatcher : Checking match of request : '/springfox/swagger-ui.html'; against '/'
2017-12-27 14:12:09.897 DEBUG 10212 --- [nio-8080-exec-1] o.s.s.w.u.matcher.AntPathRequestMatcher : Checking match of request : '/springfox/swagger-ui.html'; against '/v2/api-docs'
2017-12-27 14:12:09.897 DEBUG 10212 --- [nio-8080-exec-1] o.s.s.w.u.matcher.AntPathRequestMatcher : Checking match of request : '/springfox/swagger-ui.html'; against '/configuration/ui'
2017-12-27 14:12:09.897 DEBUG 10212 --- [nio-8080-exec-1] o.s.s.w.u.matcher.AntPathRequestMatcher : Checking match of request : '/springfox/swagger-ui.html'; against '/swagger-resources'
2017-12-27 14:12:09.897 DEBUG 10212 --- [nio-8080-exec-1] o.s.s.w.u.matcher.AntPathRequestMatcher : Checking match of request : '/springfox/swagger-ui.html'; against '/configuration/security'
2017-12-27 14:12:09.897 DEBUG 10212 --- [nio-8080-exec-1] o.s.s.w.u.matcher.AntPathRequestMatcher : Checking match of request : '/springfox/swagger-ui.html'; against '/swagger-ui.html'
2017-12-27 14:12:09.897 DEBUG 10212 --- [nio-8080-exec-1] o.s.s.w.u.matcher.AntPathRequestMatcher : Checking match of request : '/springfox/swagger-ui.html'; against '/webjars/**'
2017-12-27 14:12:09.897 DEBUG 10212 --- [nio-8080-exec-1] o.s.s.w.u.matcher.AntPathRequestMatcher : Request 'GET /springfox/swagger-ui.html' doesn't match 'POST /login
2017-12-27 14:12:09.897 DEBUG 10212 --- [nio-8080-exec-1] o.s.s.w.a.i.FilterSecurityInterceptor : Secure object: FilterInvocation: URL: /springfox/swagger-ui.html; Attributes: [authenticated]
2017-12-27 14:12:09.897 DEBUG 10212 --- [nio-8080-exec-1] o.s.s.w.a.i.FilterSecurityInterceptor : Previously Authenticated: org.springframework.security.authentication.UsernamePasswordAuthenticationToken#8f3b828e: Principal: 0001; Credentials: [PROTECTED]; Authenticated: true; Details: null; Granted Authorities: ROLE_ADMIN, ROLE_USER
2017-12-27 14:12:09.903 DEBUG 10212 --- [nio-8080-exec-1] o.s.s.access.vote.AffirmativeBased : Voter: org.springframework.security.web.access.expression.WebExpressionVoter#45d0a23, returned: 1
2017-12-27 14:12:09.903 DEBUG 10212 --- [nio-8080-exec-1] o.s.s.w.a.i.FilterSecurityInterceptor : Authorization successful
2017-12-27 14:12:09.903 DEBUG 10212 --- [nio-8080-exec-1] o.s.s.w.a.i.FilterSecurityInterceptor : RunAsManager did not change Authentication object
2017-12-27 14:12:09.903 DEBUG 10212 --- [nio-8080-exec-1] o.s.security.web.FilterChainProxy : /springfox/swagger-ui.html reached end of additional filter chain; proceeding with original chain
2017-12-27 14:12:09.904 DEBUG 10212 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : DispatcherServlet with name 'dispatcherServlet' processing GET request for [/springfox/swagger-ui.html]
2017-12-27 14:12:09.906 DEBUG 10212 --- [nio-8080-exec-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path /springfox/swagger-ui.html
2017-12-27 14:12:09.919 DEBUG 10212 --- [nio-8080-exec-1] .m.m.a.ExceptionHandlerExceptionResolver : Resolving exception from handler [null]: org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'GET' not supported
2017-12-27 14:12:09.920 DEBUG 10212 --- [nio-8080-exec-1] .w.s.m.a.ResponseStatusExceptionResolver : Resolving exception from handler [null]: org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'GET' not supported
2017-12-27 14:12:09.920 DEBUG 10212 --- [nio-8080-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolving exception from handler [null]: org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'GET' not supported
2017-12-27 14:12:09.920 WARN 10212 --- [nio-8080-exec-1] o.s.web.servlet.PageNotFound : Request method 'GET' not supported
2017-12-27 14:12:09.921 DEBUG 10212 --- [nio-8080-exec-1] w.c.HttpSessionSecurityContextRepository : SecurityContext 'org.springframework.security.core.context.SecurityContextImpl#8f3b828e: Authentication: org.springframework.security.authentication.UsernamePasswordAuthenticationToken#8f3b828e: Principal: 0001; Credentials: [PROTECTED]; Authenticated: true; Details: null; Granted Authorities: ROLE_ADMIN, ROLE_USER' stored to HttpSession: 'org.apache.catalina.session.StandardSessionFacade#3bcccd7c
2017-12-27 14:12:09.921 DEBUG 10212 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
2017-12-27 14:12:09.921 DEBUG 10212 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Successfully completed request
2017-12-27 14:12:09.922 DEBUG 10212 --- [nio-8080-exec-1] o.s.b.f.s.DefaultListableBeanFactory : Returning cached instance of singleton bean 'delegatingApplicationListener'
2017-12-27 14:12:09.923 DEBUG 10212 --- [nio-8080-exec-1] o.s.s.w.a.ExceptionTranslationFilter : Chain processed normally
2017-12-27 14:12:09.923 DEBUG 10212 --- [nio-8080-exec-1] s.s.w.c.SecurityContextPersistenceFilter : SecurityContextHolder now cleared, as request processing completed
2017-12-27 14:12:09.923 DEBUG 10212 --- [nio-8080-exec-1] o.s.b.w.f.OrderedRequestContextFilter : Cleared thread-bound request context: org.apache.catalina.connector.RequestFacade#203209de
2017-12-27 14:12:09.923 DEBUG 10212 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost] : Processing ErrorPage[errorCode=0, location=/error]
2017-12-27 14:12:09.928 DEBUG 10212 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : DispatcherServlet with name 'dispatcherServlet' processing GET request for [/error]
2017-12-27 14:12:09.928 DEBUG 10212 --- [nio-8080-exec-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path /error
2017-12-27 14:12:09.930 DEBUG 10212 --- [nio-8080-exec-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Returning handler method [public org.springframework.http.ResponseEntity io.xhub.secusid.exception.SecusidErrorHandler.error(javax.servlet.http.HttpServletRequest)]
2017-12-27 14:12:09.930 DEBUG 10212 --- [nio-8080-exec-1] o.s.b.f.s.DefaultListableBeanFactory : Returning cached instance of singleton bean 'secusidErrorHandler'
2017-12-27 14:12:09.930 DEBUG 10212 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Last-Modified value for [/error] is: -1
2017-12-27 14:12:09.943 DEBUG 10212 --- [nio-8080-exec-1] i.x.s.exception.SecusidErrorHandler : Request method 'GET' not supported
org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'GET' not supported
Try adding a class like this
#Configuration
public class WebMvcConfiguration extends WebMvcConfigurationSupport {
#Override
public void addResourceHandlers(final ResourceHandlerRegistry registry) {
// Make Swagger meta-data available via <baseURL>/v2/api-docs/
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
// Make Swagger UI available via <baseURL>/swagger-ui.html
registry.addResourceHandler("/**").addResourceLocations("classpath:/META-INF/resources/");
}
}

Spring security lead to error for PUT and POST request through Ajax

I have a small ajax request which is lead to exeption PUT http://localhost:8080/carwash/add 405 (Method Not Allowed). Can you explaim me where is a problem?
Ajax request:
$.ajax({
url: '/carwash/add',
dataType: 'json',
type: 'PUT',
success: function(data) {
this.setState({});
}.bind(this),
error: function(xhr, status, err) {
console.error('/carwash/add', status, err.toString());
}.bind(this)
})
My application is handeling by spring security which has the following configuration:
#Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/", "/login", "/logout", "/resources/*").permitAll()
.antMatchers("/owner").access("hasRole('ROLE_OWNER')")
.antMatchers("/admin").access("hasRole('ROLE_ADMIN')")
.antMatchers("/carwash").access("hasRole('ROLE_OWNER')")
.and().formLogin().loginPage("/login").successHandler(authenticationSuccessHandler)
.usernameParameter("username").passwordParameter("password")
.and().exceptionHandling().accessDeniedPage("/login?error");
}
NB: user that call ajax requst has role 'ROLE_OWNER' and can reach "/owner" and "/carwash" without problem.
Also my controller which is resposible for put request:
#RequestMapping(value = "/carwash/add", method = RequestMethod.POST)
#ResponseStatus(HttpStatus.OK)
#ResponseBody
public void addCarWashPOST(){
System.out.println("inside addCarWash");
}
#RequestMapping(value = "/carwash/add", method = RequestMethod.PUT)
#ResponseStatus(HttpStatus.OK)
#ResponseBody
public void addCarWashPUT(){
System.out.println("inside addCarWash");
}
All my application is based on spring boot therefore I have also log after this PUT request:
DEBUG 12640 --- [nio-8080-exec-9] o.s.web.servlet.DispatcherServlet : Successfully completed request
DEBUG 12640 --- [io-8080-exec-10] o.s.web.servlet.DispatcherServlet : DispatcherServlet with name 'dispatcherServlet' processing PUT request for [/login]
DEBUG 12640 --- [io-8080-exec-10] s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path /login
DEBUG 12640 --- [io-8080-exec-10] .m.m.a.ExceptionHandlerExceptionResolver : Resolving exception from handler [null]: org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'PUT' not supported
DEBUG 12640 --- [io-8080-exec-10] .w.s.m.a.ResponseStatusExceptionResolver : Resolving exception from handler [null]: org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'PUT' not supported
DEBUG 12640 --- [io-8080-exec-10] .w.s.m.s.DefaultHandlerExceptionResolver : Resolving exception from handler [null]: org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'PUT' not supported
WARN 12640 --- [io-8080-exec-10] o.s.web.servlet.PageNotFound : Request method 'PUT' not supported
DEBUG 12640 --- [io-8080-exec-10] o.s.web.servlet.DispatcherServlet : Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
DEBUG 12640 --- [io-8080-exec-10] o.s.web.servlet.DispatcherServlet : Successfully completed request
DEBUG 12640 --- [io-8080-exec-10] o.s.web.servlet.DispatcherServlet : DispatcherServlet with name 'dispatcherServlet' processing PUT request for [/error]
DEBUG 12640 --- [io-8080-exec-10] s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path /error
DEBUG 12640 --- [io-8080-exec-10] s.w.s.m.m.a.RequestMappingHandlerMapping : Returning handler method [public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)]
DEBUG 12640 --- [io-8080-exec-10] o.s.web.cors.DefaultCorsProcessor : Skip CORS processing: request is from same origin
DEBUG 12640 --- [io-8080-exec-10] o.s.w.s.m.m.a.HttpEntityMethodProcessor : Written [{timestamp=Wed Sep 14 20:17:20 CEST 2016, status=405, error=Method Not Allowed, exception=org.springframework.web.HttpRequestMethodNotSupportedException, message=Request method 'PUT' not supported, path=/carwash/add}] as "application/json;charset=UTF-8" using [org.springframework.http.converter.json.MappingJackson2HttpMessageConverter#78b6776e]
DEBUG 12640 --- [io-8080-exec-10] o.s.web.servlet.DispatcherServlet : Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
DEBUG 12640 --- [io-8080-exec-10] o.s.web.servlet.DispatcherServlet : Successfully completed request
One more interesting issue, if substitute in ajax request type to POST the exeption will be different: /carwash/add parsererror SyntaxError: Unexpected token < in JSON at position 0
The log from spring boot for POST request:
DEBUG 13348 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet : DispatcherServlet with name 'dispatcherServlet' processing GET request for [/login]
DEBUG 13348 --- [nio-8080-exec-2] s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path /login
DEBUG 13348 --- [nio-8080-exec-2] s.w.s.m.m.a.RequestMappingHandlerMapping : Returning handler method [public java.lang.String biz.controllers.mvc.LoginController.login()]
DEBUG 13348 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet : Last-Modified value for [/login] is: -1
DEBUG 13348 --- [nio-8080-exec-2] o.s.w.servlet.view.BeanNameViewResolver : No matching bean found for view name 'login'
DEBUG 13348 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet : Rendering view [org.springframework.web.servlet.view.InternalResourceView: name 'login'; URL [WEB-INF/pages/login.html]] in DispatcherServlet with name 'dispatcherServlet'
DEBUG 13348 --- [nio-8080-exec-2] o.s.w.servlet.view.InternalResourceView : Forwarding to resource [WEB-INF/pages/login.html] in InternalResourceView 'login'
DEBUG 13348 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet : DispatcherServlet with name 'dispatcherServlet' processing GET request for [/WEB-INF/pages/login.html]
DEBUG 13348 --- [nio-8080-exec-2] s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path /WEB-INF/pages/login.html
DEBUG 13348 --- [nio-8080-exec-2] s.w.s.m.m.a.RequestMappingHandlerMapping : Did not find handler method for [/WEB-INF/pages/login.html]
DEBUG 13348 --- [nio-8080-exec-2] o.s.w.s.handler.SimpleUrlHandlerMapping : Matching patterns for request [/WEB-INF/pages/login.html] are [/**]
DEBUG 13348 --- [nio-8080-exec-2] o.s.w.s.handler.SimpleUrlHandlerMapping : URI Template variables for request [/WEB-INF/pages/login.html] are {}
DEBUG 13348 --- [nio-8080-exec-2] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapping [/WEB-INF/pages/login.html] to HandlerExecutionChain with handler [org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler#1aa2d29f] and 1 interceptor
DEBUG 13348 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet : Last-Modified value for [/WEB-INF/pages/login.html] is: -1
DEBUG 13348 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet : Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
DEBUG 13348 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet : Successfully completed request
DEBUG 13348 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet : Successfully completed request
and as I undestand from log, the application cannot No matching bean found for view name 'login' how ever I have a controller for #RequestMapping(value ={"/", "/login"}, method = RequestMethod.GET) and without problem can open page with link /login... What I miss ?
Controller for "/" and "/login"
#RequestMapping(value ={"/", "/login"}, method = RequestMethod.GET)
public String login() {
return "login";
}
Configuration:
#Bean
public InternalResourceViewResolver viewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("WEB-INF/pages/");
resolver.setSuffix(".html");
return resolver;
}
To make an AJAX/JSON request with CSRF enabled you have to pass CSRF token as a HTTP Request Header, not a parameter or other data.
On the page, your meta tags should look like these:
<meta name="_csrf" content="${_csrf.token}"/>
<meta name="_csrf_header" content="${_csrf.headerName}"/>
Then, prepare values somewhere in the JS code:
var token = $("meta[name='_csrf']").attr("content");
var header = $("meta[name='_csrf_header']").attr("content");
Pass the CSRF token as a header:
$.ajax({
type: "PUT",
async: false,
url: '/carwash/add',
data: [your data],
beforeSend: function(xhr) {
// here it is
xhr.setRequestHeader(header, token);
},
success: function(obj) {
// ....
},
....
You can see more here
http://docs.spring.io/spring-security/site/docs/current/reference/html/csrf.html

spring boot runs but the pages are not accessible

When I access my app via browser I get an 404 (Whitelabel Error Page).
After searching around, I think it could be a problem with my sitemesh filter.
#Bean
public FilterRegistrationBean sitemesh() throws Exception {
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
filterRegistrationBean.setFilter(new MySiteMeshFilter());
return filterRegistrationBean;
}
MySitemeshFilter:
protected static class MySiteMeshFilter extends ConfigurableSiteMeshFilter {
protected void applyCustomConfiguration(SiteMeshFilterBuilder builder) {
builder.addDecoratorPath("/*", "/decorators/basic-theme.jsp");
}
}
What do you think? Thank you!
EDIT:
It could be also possible, that my Security Configuration is the problem:
#Configuration
#Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
public class ApplicationSecurity extends WebSecurityConfigurerAdapter {
#Autowired
private MongoTemplate mongoTemplate;
#Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/", "/error").permitAll()
.antMatchers("/register*", "/login", "/lostPassword").anonymous()
.antMatchers("/admin","/admin**").hasRole("ADMIN")
.antMatchers("/user", "/user/**", "/offer/*", "/page", "/page/**").hasAnyRole("USER", "COMPANY_USER", "COMPANY_MASTER", "ADMIN")
.antMatchers("/company", "/company/**").hasAnyRole("COMPANY_USER", "COMPANY_MASTER", "ADMIN")
.anyRequest().authenticated()
.and()
.formLogin().loginPage("/login").failureUrl("/login?error=true").usernameParameter("username").passwordParameter("password").loginProcessingUrl("/security_check")
.successHandler(new MyAuthenticationSuccessHandler(this.mongoTemplate));
http.logout().logoutUrl("/logout").invalidateHttpSession(true).logoutSuccessUrl("/");
}
#Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(new MyUserDetailsService(this.mongoTemplate)).passwordEncoder(new ShaPasswordEncoder(256));
}
}
EDIT 2:
The controllers are not resolved. Setting the #ComponentScan "basePackages" attribute helps. The controller-methods are now being executed. But it looks like spring boot cannot find my views(directory) to render the page.
EDIT 3:
This is a part of my logging output. Is there anything unusual? It starts with INFO log (log4j) "starting page" which I put in the method ("/") of my controller. The controller method belonging to the RequestMapping is fired, but I think spring boot cannot find the jsp.
2014-12-30 10:20:09.422 INFO 5884 --- [nio-8080-exec-3] c.l.c.controller.PublicController : starting page
2014-12-30 10:20:09.426 DEBUG 5884 --- [nio-8080-exec-3] o.s.w.s.v.ContentNegotiatingViewResolver : Requested media types are [text/html, application/xhtml+xml, application/xml;q=0.9, */*;q=0.8] based on Accept header types and producible media types [*/*])
2014-12-30 10:20:09.427 DEBUG 5884 --- [nio-8080-exec-3] o.s.w.servlet.view.BeanNameViewResolver : No matching bean found for view name 'home'
2014-12-30 10:20:09.427 DEBUG 5884 --- [nio-8080-exec-3] o.s.w.s.v.ContentNegotiatingViewResolver : Returning [org.springframework.web.servlet.view.JstlView: name 'home'; URL [/WEB-INF/views/home.jsp]] based on requested media type 'text/html'
2014-12-30 10:20:09.427 DEBUG 5884 --- [nio-8080-exec-3] o.s.web.servlet.DispatcherServlet : Rendering view [org.springframework.web.servlet.view.JstlView: name 'home'; URL [/WEB-INF/views/home.jsp]] in DispatcherServlet with name 'dispatcherServlet'
2014-12-30 10:20:09.433 DEBUG 5884 --- [nio-8080-exec-3] o.s.web.servlet.view.JstlView : Forwarding to resource [/WEB-INF/views/home.jsp] in InternalResourceView 'home'
2014-12-30 10:20:09.435 DEBUG 5884 --- [nio-8080-exec-3] o.s.web.servlet.DispatcherServlet : DispatcherServlet with name 'dispatcherServlet' processing GET request for [/WEB-INF/views/home.jsp]
2014-12-30 10:20:09.436 DEBUG 5884 --- [nio-8080-exec-3] s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path /WEB-INF/views/home.jsp
2014-12-30 10:20:09.446 DEBUG 5884 --- [nio-8080-exec-3] s.w.s.m.m.a.RequestMappingHandlerMapping : Did not find handler method for [/WEB-INF/views/home.jsp]
2014-12-30 10:20:09.446 DEBUG 5884 --- [nio-8080-exec-3] o.s.w.s.handler.SimpleUrlHandlerMapping : Matching patterns for request [/WEB-INF/views/home.jsp] are [/**]
2014-12-30 10:20:09.446 DEBUG 5884 --- [nio-8080-exec-3] o.s.w.s.handler.SimpleUrlHandlerMapping : URI Template variables for request [/WEB-INF/views/home.jsp] are {}
2014-12-30 10:20:09.447 DEBUG 5884 --- [nio-8080-exec-3] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapping [/WEB-INF/views/home.jsp] to HandlerExecutionChain with handler [ResourceHttpRequestHandler [locations=[ServletContext resource [/], class path resource [META-INF/resources/], class path resource [resources/], class path resource [static/], class path resource [public/]], resolvers=[org.springframework.web.servlet.resource.PathResourceResolver#727d5eda]]] and 1 interceptor
2014-12-30 10:20:09.448 DEBUG 5884 --- [nio-8080-exec-3] o.s.web.servlet.DispatcherServlet : Last-Modified value for [/WEB-INF/views/home.jsp] is: -1
2014-12-30 10:20:09.448 DEBUG 5884 --- [nio-8080-exec-3] o.s.web.servlet.DispatcherServlet : Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
2014-12-30 10:20:09.448 DEBUG 5884 --- [nio-8080-exec-3] o.s.web.servlet.DispatcherServlet : Successfully completed request
2014-12-30 10:20:09.449 DEBUG 5884 --- [nio-8080-exec-3] o.s.web.servlet.DispatcherServlet : Successfully completed request
2014-12-30 10:20:09.450 DEBUG 5884 --- [nio-8080-exec-3] o.s.web.servlet.DispatcherServlet : DispatcherServlet with name 'dispatcherServlet' processing GET request for [/error]
2014-12-30 10:20:09.451 DEBUG 5884 --- [nio-8080-exec-3] s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path /error
2014-12-30 10:20:09.452 DEBUG 5884 --- [nio-8080-exec-3] s.w.s.m.m.a.RequestMappingHandlerMapping : Returning handler method [public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest)]
2014-12-30 10:20:09.452 DEBUG 5884 --- [nio-8080-exec-3] o.s.web.servlet.DispatcherServlet : Last-Modified value for [/error] is: -1
2014-12-30 10:20:09.453 DEBUG 5884 --- [nio-8080-exec-3] o.s.w.s.v.ContentNegotiatingViewResolver : Requested media types are [text/html, text/html;q=0.8] based on Accept header types and producible media types [text/html])
2014-12-30 10:20:09.453 DEBUG 5884 --- [nio-8080-exec-3] o.s.w.s.v.ContentNegotiatingViewResolver : Returning [org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$SpelView#5dbc9982] based on requested media type 'text/html'
2014-12-30 10:20:09.453 DEBUG 5884 --- [nio-8080-exec-3] o.s.web.servlet.DispatcherServlet : Rendering view [org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$SpelView#5dbc9982] in DispatcherServlet with name 'dispatcherServlet'
2014-12-30 10:20:09.454 DEBUG 5884 --- [nio-8080-exec-3] o.s.web.servlet.DispatcherServlet : Successfully completed request
2014-12-30 10:20:09.480 DEBUG 5884 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet : DispatcherServlet with name 'dispatcherServlet' processing GET request for [/favicon.ico]
2014-12-30 10:20:09.481 DEBUG 5884 --- [nio-8080-exec-4] o.s.w.s.handler.SimpleUrlHandlerMapping : Matching patterns for request [/favicon.ico] are [/**/favicon.ico]
2014-12-30 10:20:09.481 DEBUG 5884 --- [nio-8080-exec-4] o.s.w.s.handler.SimpleUrlHandlerMapping : URI Template variables for request [/favicon.ico] are {}
2014-12-30 10:20:09.481 DEBUG 5884 --- [nio-8080-exec-4] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapping [/favicon.ico] to HandlerExecutionChain with handler [ResourceHttpRequestHandler [locations=[class path resource [META-INF/resources/], class path resource [resources/], class path resource [static/], class path resource [public/], class path resource []], resolvers=[org.springframework.web.servlet.resource.PathResourceResolver#1742114b]]] and 1 interceptor
2014-12-30 10:20:09.481 DEBUG 5884 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet : Last-Modified value for [/favicon.ico] is: -1
2014-12-30 10:20:09.490 DEBUG 5884 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet : Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
2014-12-30 10:20:09.491 DEBUG 5884 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet : Successfully completed request
In my application.properties I added:
spring.view.prefix=/WEB-INF/views/
spring.view.suffix=.jsp
For any of you who stumbled here looking for an answer to 'o.s.w.servlet.view.BeanNameViewResolver : No matching bean found for view name 'home'', Spring has tried and failed to locate a view resolver for 'home'.
If it is pure html you want to serve, you need just return '/home.html' from your controller. Spring will look for this file under /src/main/resources/static and return a response accordingly.
For other ways to resolve views, read this excellent answer, How to map requests to HTML file in Spring MVC?

Resources