16

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)
en Lopes
  • 1,863
  • 11
  • 48
  • 90
  • 11
    If anybody is still interested in obtaining a `WebRequest` from a `HttpServletRequest` , here is how: `WebRequest webRequest = new ServletWebRequest(request);` – Dan Manastireanu Apr 15 '19 at 13:57
  • 1
    And how about vice versa. HttpServletRequest from WebRequest – VIJ Jul 25 '19 at 05:57

1 Answers1

30

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)

}
shazin
  • 21,379
  • 3
  • 54
  • 71
  • 4
    You can remove RequestAttributes requestAttributes = new ServletRequestAttributes(request); and the HttpServletRequest from getErrorAtributes aswell – Toofy Jan 25 '18 at 23:51