Spring MVC-我可以在RestController中自动装配HttpServletRequest吗 [英] Spring MVC - Can I autowire HttpServletRequest in RestController

查看:607
本文介绍了Spring MVC-我可以在RestController中自动装配HttpServletRequest吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是否可以像下面这样在RestController中自动装配HttpServletRequest,即使在高度并发的环境中执行,它也会返回不同的servletRequest.我有一个限制,我不能将其作为方法参数,因为我实现的是一个自动生成的接口,并且不会将HttpServletRequest作为方法参数.

Can I autowire HttpServletRequest in my RestController like following and will it returns different servletRequest even if it is executed in highly concurrent environment. I have a restriction that I can not have as method parameter because I am implementing an interface which is auto generated and will not have HttpServletRequest as the method parameter.

@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 ?

I have gone through these SO questions and some other articles on this. But just wanted to ensure that when we autowire HttpServletRequest in the controller then its Scope is Request?

Spring 3 MVC从控制器访问HttpRequest

如何分配线程来处理Servlet请求?

Spring控制器的范围及其实例变量

如何在春季获得HttpServletRequest豆吗?

如何获取HTTP请求标头在Java中

注意::我确实尝试过此方法,但似乎工作正常.但是只是想确认这是一个万无一失的解决方案,即使在高度并发的环境中也是如此. 同样,如果这是正确的方法,那么如果有人可以解释它是如何工作的,我将不胜感激.

Note: I did try this and it seems to work fine. But just wanted to confirm that it's a foolproof solution even in a highly concurrent environment. Also if this is the correct way to do it, I would appreciate if someone can explain how exactly it works.

推荐答案

我使用了它,并且效果很好.但不幸的是,我没有找到任何官方文件提及此功能.

I have used this and it works fine. But unfortunately I did not find any official documentation which mentions that this should work.

以下是根据我的理解进行的解释,即通过运行具有不同标头/有效负载等的多个请求来调试代码:

Here is the explanation based on my understanding from debugging the code with running multiple requests with different headers/payload etc.:

无论是在现场还是通过构造函数自动布线,servletRequest都像一个Proxy对象,将调用委派给 Current HttpServletRequest ,这对于每个请求都是不同的.因此,即使通过构造函数将其注入到Singleton RestController 中,对于每个新请求,该调用仍将委派给相应的HttpServletRequest.这利用了

Whether we autowire on field or through the constructor, servletRequest acts like a Proxy object which delegates the call to Current HttpServletRequest which is different for each request. Thus, even though it's injected through constructor in a Singleton RestController, it will still have the call delegated to corresponding HttpServletRequest for each new request. This utilizes AutowireUtils.ObjectFactoryDelegatingInvocationHandler to access the current HttpServletRequest object. The java doc for it also says Reflective InvocationHandler for lazy access to the current target object.

因此,即使所有请求的自动连接的Proxy对象始终相同,将调用委派给其的基础目标对象还是每个请求的当前HttpServletRequest对象.

Thus even if the autowired Proxy object is always the same for all requests, the underlying target object to which the call is delegated is the current HttpServletRequest object which is per-request.

还有一种获取HttpServletRequest的方法是使用RequestContextHolder,如此答案

There is another way you can get HttpServletRequest is using RequestContextHolder as mentioned in this answer

HttpServletRequest currentRequest = 
((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes())
.getRequest();

注意:由于此说明是基于我的理解,因此请共享任何有关此内容的官方文档.

Note: As this explanation is based on my understanding, please do share any official documentation about this if anyone has it.

这篇关于Spring MVC-我可以在RestController中自动装配HttpServletRequest吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆