I've generated a Spring Boot web application using Spring Initializer, embedded Tomcat, Thymeleaf template engine, and package as an executable JAR file.
Technologies used:
Spring Boot 2.0.0.M6 , Java 8, maven
I have this method in 1 of the class
private Map<String, Object> getErrorAttributes(HttpServletRequest request,
boolean includeStackTrace) {
RequestAttributes requestAttributes = new ServletRequestAttributes(request);
return this.errorAttributes.getErrorAttributes(request, includeStackTrace)
}
But I don't know how to cast from javax.servlet.http HttpServletRequest
org.springframework.web.context.request.WebRequest
The method getErrorAttributes(WebRequest, boolean) in the type ErrorAttributes is not applicable for the arguments (HttpServletRequest,
boolean)
Casting WebRequest to ServletWebRequest solved the purpose. Instead of getRequestURL() , getRequestURI() be used to get the URI in the question.
public interface WebRequest extends RequestAttributes. Generic interface for a web request. Mainly intended for generic web request interceptors, giving them access to general request metadata, not for actual handling of the request.
The code to get it is as follows. ServletRequestAttributes requestAttributes = (ServletRequestAttributes)RequestContextHolder. getRequestAttributes(); // get the request HttpServletRequest request = requestAttributes. getRequest();
HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder. getRequestAttributes()) . getRequest();
You don't need to cast HttpServletRequest
to WebRequest
. What you need is using WebRequest
in your controller method.
@GetMapping("/endpoint")
public .. endpont(HttpServletRequest request, WebRequest webRequest) {
getErrorAttributes(request, webRequest, true);
}
And change to your getErrorAttributes
method
private Map<String, Object> getErrorAttributes(HttpServletRequest request, WebRequest webRequest,
boolean includeStackTrace) {
RequestAttributes requestAttributes = new ServletRequestAttributes(request);
return this.errorAttributes.getErrorAttributes(webRequest, includeStackTrace)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With