@Autowired HttpServletRequest 的 SpringBoot 转发请求导致 StackOverFlow 异常

SpringBoot forward request by @Autowired HttpServletRequest result in StackOverFlow Exception

我有一个这样的@Controller Class:

@Controller
@RequestMapping(path = "/test")
public class TestController {
    @Autowired
    HttpServletRequest httpServletRequest;
    @Autowired
    HttpServletResponse httpServletResponse;

    @GetMapping(path = "hello")
    @ResponseBody
    public String hello(@RequestParam String name) {
        return "HELLO " + name;
    }

    @GetMapping(path = "forward")
    public void forward(
            HttpServletRequest request,
            HttpServletResponse response,
            @RequestParam String name) {
        try {
            httpServletRequest.getRequestDispatcher ( "/test/hello" )
                    .forward ( httpServletRequest, httpServletResponse);
        } catch (ServletException e) {
            e.printStackTrace ();
        } catch (IOException e) {
            e.printStackTrace ();
        }
    }

}

当我请求 URI /test/forward?name=someone 时,导致以下异常:

2020-02-25 18:36:04.520 ERROR 86384 --- [io-12345-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] threw exception

java.lang.WhosebugError: null
    at java.lang.Exception.<init>(Exception.java:102) ~[na:1.8.0_131]
    at java.lang.ReflectiveOperationException.<init>(ReflectiveOperationException.java:89) ~[na:1.8.0_131]
    at java.lang.reflect.InvocationTargetException.<init>(InvocationTargetException.java:72) ~[na:1.8.0_131]
    at sun.reflect.GeneratedMethodAccessor22.invoke(Unknown Source) ~[na:na]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_131]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_131]
    at org.springframework.beans.factory.support.AutowireUtils$ObjectFactoryDelegatingInvocationHandler.invoke(AutowireUtils.java:294) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at com.sun.proxy.$Proxy57.setAttribute(Unknown Source) ~[na:na]
    at org.apache.catalina.core.ApplicationHttpRequest.setAttribute(ApplicationHttpRequest.java:306) ~[tomcat-embed-core-9.0.30.jar:9.0.30]
    at sun.reflect.GeneratedMethodAccessor22.invoke(Unknown Source) ~[na:na]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_131]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_131]
    at org.springframework.beans.factory.support.AutowireUtils$ObjectFactoryDelegatingInvocationHandler.invoke(AutowireUtils.java:294) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at com.sun.proxy.$Proxy57.setAttribute(Unknown Source) ~[na:na]
    at org.apache.catalina.core.ApplicationHttpRequest.setAttribute(ApplicationHttpRequest.java:306) ~[tomcat-embed-core-9.0.30.jar:9.0.30]
    at sun.reflect.GeneratedMethodAccessor22.invoke(Unknown Source) ~[na:na]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_131]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_131]

有人可以帮我排除异常吗?

删除 @Autowired request/response 字段并使用您的 request/response 参数。

@GetMapping(path = "forward")
    public void forward(
            HttpServletRequest request,
            HttpServletResponse response,
            @RequestParam String name) {
        try {
            request.getRequestDispatcher ( "/test/hello" )
                    .forward (request, response);
        } catch (ServletException e) {
            e.printStackTrace ();
        } catch (IOException e) {
            e.printStackTrace ();
        }
    }

request/response 字段注入导致奇怪的行为,因为 Spring 用 @Controller 注释的 Bean 默认具有范围单例,因此每个请求将由单例处理 TestController 实例。