Casting from HttpServletRequest to WebRequest

en Lopes :

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)
shazin :

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)

}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=437980&siteId=1