(ServletRequestAttributes)RequestContextHolder.getRequestAttributes().getResponse()
时间: 2023-11-08 22:51:24 浏览: 144
TrafficControl.js
回答: 在代码(ServletRequestAttributes)RequestContextHolder.getRequestAttributes().getResponse()中,会报空指针异常java.lang.NullPointerException: null。原因是getRequestAttributes()方法返回的是null,而getResponse()方法需要一个非空的ServletRequestAttributes对象作为参数。为了解决这个问题,可以参考第二种解决办法,在开启新线程之前,将RequestAttributes对象设置为子线程共享。具体做法是在开启新线程之前添加以下代码:
ServletRequestAttributes sra = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
RequestContextHolder.setRequestAttributes(sra, true);\[2\]这样就可以在新线程中获取到非空的ServletRequestAttributes对象,进而调用getResponse()方法。
#### 引用[.reference_title]
- *1* [RequestContextHolder.getRequestAttributes()空指针](https://blog.csdn.net/weixin_43838174/article/details/129580675)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item]
- *2* [RequestContextHolder.getRequestAttributes()子线程获取问题](https://blog.csdn.net/ypp91zr/article/details/114395178)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item]
- *3* [Feign拦截器中获取RequestContextHolder.getRequestAttributes()为空问题排查](https://blog.csdn.net/chengpei147/article/details/121385433)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
阅读全文