一般在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);
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)