【发布时间】:2018-09-15 18:25:37
【问题描述】:
我可以像下面那样在我的RestController 中自动连接HttpServletRequest,即使它在高度并发的环境中执行,它是否会返回不同的servletRequest。我有一个不能作为方法参数的限制,因为我正在实现一个自动生成的接口,并且不会将HttpServletRequest 作为方法参数。
@RestController
public class MyController implements MyInterface {
@Autowired
private HttpServletRequest servletRequest;
@Override
@RequestMapping(value = "/test", produces = {"application/json"}, consumes = {"application/json"}, method = RequestMethod.POST)
public ResponseEntity<MyResponse> test(@RequestBody final MyRequest payload){
...
}
...
}
我已经阅读了这些 SO 问题和其他一些关于此的文章。但只是想确保当我们在控制器中自动连接 HttpServletRequest 时,它的 Scope 是 Request?
Spring 3 MVC accessing HttpRequest from controller
How are Threads allocated to handle Servlet request?
Scope of a Spring-Controller and its instance-variables
How do I get a HttpServletRequest in my spring beans?
How To Get HTTP Request Header In Java
注意:我确实尝试过,它似乎工作正常。但只是想确认即使在高度并发的环境中它也是一个万无一失的解决方案。 此外,如果这是正确的方法,如果有人能解释它是如何工作的,我将不胜感激。
【问题讨论】:
-
为什么不
public ResponseEntity<MyResponse> test(@RequestBody final MyRequest payload, HttpServletRequest request) {...}? Spring不会自动注入请求吗? -
我确实在我的问题中提到我不能这样做。因为我需要在我的
RestController中实现MyInterface,它是从规范自动生成的,它不能将HttpServletRequest作为方法参数。
标签: spring-mvc scope httprequest