@ Autowired HttpServletRequest的SpringBoot转发请求导致StackOverFlow异常
来源:爱站网时间:2021-09-16编辑:网友分享
我有一个@Controller类,例如:@Controller @RequestMapping(path =“ / test”)公共类TestController {@Autowired HttpServletRequest httpServletRequest; @Autowired ...
问题描述
我有一个@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.(Exception.java:102) ~[na:1.8.0_131]
at java.lang.ReflectiveOperationException.(ReflectiveOperationException.java:89) ~[na:1.8.0_131]
at java.lang.reflect.InvocationTargetException.(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
实例处理。