Search code examples
javaspring-bootcontrollerautowiredforward

SpringBoot forward request by @Autowired HttpServletRequest result in StackOverFlow Exception


I have a @Controller Class like this:

@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 ();
        }
    }

}

when I request the URI /test/forward?name=someone, result in the exception below:

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.StackOverflowError: 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]

Somebody can help me out of the exception?


Solution

  • Remove the @Autowired request/response fields and use your request/response parameters.

    @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 ();
            }
        }
    

    The request/response fields injection is causing an odd behavior due to Spring Beans annotated with @Controller has scope singleton by default, therefore each request will be handle by a singleton TestController instance.