HttpServletRequest在spring中获取及使用

HttpServletRequest在spring中获取及使用,第1张

HttpServletRequest在spring中获取及使用

    一般在controller 中直接获取:HttpServletRequest

    HttpServletRequest 是线程安全的,所以在service层可以直接通过注入使用

@Autowired
private HttpServletRequest servletRequest;
    业务逻辑中,也可通过RequestContextHolder 获取
ServletRequestAttributes requestAttributes1 = (ServletRequestAttributes) 

HttpServletRequest servletRequest = requestAttributes1.getRequest(); 

HttpServletResponse response = requestAttributes1.getResponse();

父线程传递请求信息到子线程用法

若在子线程中需要传递主线程的请求头信息,可以在主线程获取RequestAttributes

RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();

在子线程设置:

RequestContextHolder.setRequestAttributes(requestAttributes);

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5712934.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存