@ Autowired HttpServletRequest的SpringBoot转发请求导致StackOverFlow异常
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了@ Autowired HttpServletRequest的SpringBoot转发请求导致StackOverFlow异常相关的知识,希望对你有一定的参考价值。
我有一个@Controller类,如:
@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.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]
有人可以帮我一个例外吗?
答案
删除@Autowired
请求/响应字段并使用您的请求/响应参数。
@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 ();
}
}
由于通过@Controller
注释的Spring Bean默认情况下具有作用域单例,因此请求/响应字段注入导致异常行为,因此每个请求将由一个单例TestController
实例处理。
以上是关于@ Autowired HttpServletRequest的SpringBoot转发请求导致StackOverFlow异常的主要内容,如果未能解决你的问题,请参考以下文章