从HttpServletRequest转换为WebRequest

时间:2017-11-23 12:36:34

标签: java spring spring-mvc spring-boot

我使用Spring Initializer,嵌入式Tomcat,Thymeleaf模板引擎和包作为可执行的JAR文件生成了一个Spring Boot Web应用程序。

使用的技术:

Spring Boot 2.0.0.M6,Java 8,maven

我在类<1>中有这个方法

private Map<String, Object> getErrorAttributes(HttpServletRequest request,
                                                   boolean includeStackTrace) {

        RequestAttributes requestAttributes = new ServletRequestAttributes(request);
        return this.errorAttributes.getErrorAttributes(request, includeStackTrace)

    }

但我不知道如何从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)

1 个答案:

答案 0 :(得分:18)

您不需要将HttpServletRequest投射到WebRequest。您需要的是在控制器方法中使用WebRequest

@GetMapping("/endpoint")
public .. endpont(HttpServletRequest request, WebRequest webRequest) {
    getErrorAttributes(request, webRequest, true);
}

并更改为getErrorAttributes方法

private Map<String, Object> getErrorAttributes(HttpServletRequest request, WebRequest webRequest,
                                               boolean includeStackTrace) {

    RequestAttributes requestAttributes = new ServletRequestAttributes(request);
    return this.errorAttributes.getErrorAttributes(webRequest, includeStackTrace)

}