SpringMVC对应的controller方法中,如何获取request和response。一种是作为参数传递进来,一种是使用SpringMVC提供的RequestContextHolder去获取。例如下面代码
@RequestMapping("/helloResponseBody")
@ResponseBody
public String helloResponseBody(){
HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
logger.info("request = " + request);
return UUID.randomUUID().toString();
}
这里,主要是介绍RequestContextHolder的原理,假定使用SpringMVC提供的标准的servlet类DispatcherServlet,容器是Tomcat。
package org.springframework.web.context.request;
只保留关键代码
public abstract class RequestContextHolder {
private static final ThreadLocal<RequestAttributes> requestAttributesHolder =
new NamedThreadLocal<>("Request attributes");
public static void setRequestAttributes(@Nullable RequestAttributes attributes) {
setRequestAttributes(attributes, false);
}
public static void setRequestAttributes(@Nullable RequestAttributes attributes, boolean inheritable) {
......
requestAttributesHolder.set(attributes);
......
}
}
@Nullable
public static RequestAttributes getRequestAttributes() {
RequestAttributes attributes = requestAttributesHolder.get();
......
return attributes;
}
......
}
package org.springframework.web.servlet;
只保留关键代码
public abstract class FrameworkServlet extends HttpServletBean implements ApplicationContextAware {
/**
* Close the WebApplicationContext of this servlet.
* @see org.springframework.context.ConfigurableApplicationContext#close()
*/
@Override
public void destroy() {
......
}
/**
* Override the parent class implementation in order to intercept PATCH requests.
*/
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpMethod httpMethod = HttpMethod.resolve(request.getMethod());
if (httpMethod == HttpMethod.PATCH || httpMethod == null) {
processRequest(request, response);
}
else {
super.service(request, response);
}
}
@Override
protected final void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected final void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected final void doPut(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected final void doDelete(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doOptions(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doTrace(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Process this request, publishing an event regardless of the outcome.
* <p>The actual event handling is performed by the abstract
* {@link #doService} template method.
*/
protected final void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
LocaleContext previousLocaleContext = LocaleContextHolder.getLocaleContext();
LocaleContext localeContext = buildLocaleContext(request);
RequestAttributes previousAttributes = RequestContextHolder.getRequestAttributes();
ServletRequestAttributes requestAttributes = buildRequestAttributes(request, response, previousAttributes);
initContextHolders(request, localeContext, requestAttributes);
try {
doService(request, response);
}
catch (ServletException | IOException ex) {
failureCause = ex;
throw ex;
}
catch (Throwable ex) {
failureCause = ex;
throw new NestedServletException("Request processing failed", ex);
}
}
@Nullable
protected ServletRequestAttributes buildRequestAttributes(HttpServletRequest request,
@Nullable HttpServletResponse response, @Nullable RequestAttributes previousAttributes) {
if (previousAttributes == null || previousAttributes instanceof ServletRequestAttributes) {
return new ServletRequestAttributes(request, response);
}
else {
return null; // preserve the pre-bound RequestAttributes instance
}
}
private void initContextHolders(HttpServletRequest request,
@Nullable LocaleContext localeContext, @Nullable RequestAttributes requestAttributes) {
if (requestAttributes != null) {
RequestContextHolder.setRequestAttributes(requestAttributes, this.threadContextInheritable);
}
}
/**
* Subclasses must implement this method to do the work of request handling,
* receiving a centralized callback for GET, POST, PUT and DELETE.
* <p>The contract is essentially the same as that for the commonly overridden
* {@code doGet} or {@code doPost} methods of HttpServlet.
* <p>This class intercepts calls to ensure that exception handling and
* event publication takes place.
* @param request current HTTP request
* @param response current HTTP response
* @throws Exception in case of any kind of processing failure
* @see javax.servlet.http.HttpServlet#doGet
* @see javax.servlet.http.HttpServlet#doPost
*/
protected abstract void doService(HttpServletRequest request, HttpServletResponse response)
throws Exception;
}
核心思想,每次http请求,容器最终都会调用DispatcherServlet类的service方法,实际调用的是DispatcherServlet父类FrameworkServlet的service方法。FrameworkServlet的service方法内,可能直接调用processRequest方法,也可能调用super.service方法。即使调用父类的service方法,父类service方法最终还会调用FrameworkServlet类的doPost、doGet等相应的方法(理由见下文备注),最终结论是,无论何种形式,最终都会调用FrameworkServlet类的processRequest方法。
processRequest方法内部,会先将request和response对象封装好,然后将封装好的对象,绑定到RequestContextHolder类的ThreadLocal<RequestAttributes> requestAttributesHolder属性内。之后FrameworkServlet类的processRequest方法,调用DispatcherServlet类的doService方法。DispatcherServlet类的doService方法内部,调用DispatcherServlet类的doDispatch方法,进而走完整个请求。
简单的说,就是SpringMVC利用ThreadLocal技术,每次在处理请求前,将请求的request和response放到RequestContextHolder的ThreadLocal对象内。在后续处理请求时,可以通过RequestContextHolder类获取ThreadLocal内的request和response对象。
备注:这需要巩固一个知识点:当子类重写了父类的函数,那么子类的对象如果调用该函数,一定调用的是重写过后的函数。可以通过super关键字进行父类的重写函数的调用。
public class ATest extends AATest{
protected void doGet(){
System.out.println("ATest doGet");
}
public static void main(String[] args) {
new ATest().service();
}
}
abstract class AATest extends AAATest{
protected void service(){
System.out.println("AATest service");
doGet();
}
protected void doGet(){
System.out.println("AATest doGet");
}
}
结论:
AATest service
ATest doGet
1、异步线程RequestContextHolder.getRequestAttributes()为null
使用Spring框架,在Service中开启一个新的线程,在新的线程中使用
RequestAttributes ra = RequestContextHolder.getRequestAttributes();
获取出来为null,有没有什么办法能解决?问题出现的环境背景及自己尝试过哪些方法
Service中为优化响应时间,使用了多线程,部分线程中使用到了RequestContextHolder类,结果返回值为空。
相关代码
RequestAttributes ra = RequestContextHolder.getRequestAttributes();
ServletRequesRequestContextHolder.getRequestAttributes()tAttributes sra = (ServletRequestAttributes) ra;
HttpServletRequest request = sra.getRequest();
System.out.println(request.getContextPath());你期待的结果是什么?实际看到的错误信息又是什么?
期待:System.out.println(request.getContextPath());正常打印出路径,
实际:RequestAttributes ra = RequestContextHolder.getRequestAttributes();开启新线程之前,添加代码:
//将RequestAttributes对象设置为子线程共享
ServletRequestAttributes sra = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
RequestContextHolder.setRequestAttributes(sra, true);
https://blog.csdn.net/gaoshan12345678910/article/details/81745951
https://blog.csdn.net/u012706811/article/details/53432032(闲了总结一下)