1、SpringMVC 的控制层中通常有如下方式获取用户请求的参数:
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@RestController
public class UserController {
/**
* 传统方式
* http://localhost:8080/user/clearCache1?tenant_id=440000&token=9e3bc1ba-f7cd-4754-971d-0217841e62b8
* @param request
* @return
*/
@GetMapping("user/clearCache1")
@SuppressWarnings("all")
public String clearCache1(HttpServletRequest request) {
String tenant_id = request.getParameter("tenant_id");//参数不存在时,返回 null
String token = request.getParameter("token");
String result = "tenant_id=" + tenant_id + ",token=" + token;
return result;
}
/**
* 推荐方式
* http://localhost:8080/user/clearCache2?tenant_id=430000&token=5a9da3b3-30a5-4737-8b58-13dbed3a9a57
* @param tenant_id
* @param token
* @return
*/
@GetMapping("user/clearCache2")
public String clearCache2(Long tenant_id, String token) {
String result = "tenant_id=" + tenant_id + ",token=" + token;
return result;
}
/**
* 在控制层以外的地方,在不接收任何参数的情况下获取 HttpServletRequest、HttpServletResponse 的特殊方式
* http://localhost:8080/user/clearCache3?tenant_id=450000&token=2f2ef709-cb50-4b43-acde-07fc1cc8f4bd
* @return
*/
@GetMapping("user/clearCache3")
@SuppressWarnings("all")
public String clearCache3() {
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) requestAttributes;
HttpServletRequest request = servletRequestAttributes.getRequest();
HttpServletResponse response = servletRequestAttributes.getResponse();
String sessionId = servletRequestAttributes.getSessionId();
System.out.println("】】request=" + request);
System.out.println("】】session=" + request.getSession() + ", id=" + request.getSession().getId());
System.out.println("】】response=" + response);
System.out.println("】】sessionId=" + sessionId);
System.out.println("】】realPath=" + request.getServletContext().getRealPath("/"));
String tenant_id = request.getParameter("tenant_id");
String token = request.getParameter("token");
String result = "tenant_id=" + tenant_id + ",token=" + token;
return result;
}
}
2、这里重点介绍一下 org.springframework.web.context.request.RequestContextHolder 类 - 请求上下文支持,它在 spring-web-x.x.x.RELEASE.jar 包中。
3、RequestContextHolder 的源码中使用 ThreadLocal 维护了一个如下的静态成员变量:
private static final ThreadLocal<RequestAttributes> requestAttributesHolder = new NamedThreadLocal<>("Request attributes");
提供了如下两个静态方法来获取 org.springframework.web.context.request.RequestAttributes 接口:
static RequestAttributes currentRequestAttributes()
satic RequestAttributes getRequestAttributes()
4、RequestAttributes 接口有好几个实现类,其中常用的有 org.springframework.web.context.request.ServletRequestAttributes。它提供了获取如下对象的方法:
javax.servlet.http.HttpServletRequest
javax.servlet.http.HttpServletResponse
5、SpringMVC 利用线程局部变量 ThreadLocal,每次在处理请求前,将请求的 request 和 response 放到 RequestContextHolder 的 ThreadLocal 对象内,后续处理请求时,可以再通过 RequestContextHolder 类获取。
(本文演示环境为:Java JDK 1.8 + Spring Boot 2.0.3(spring-boot-starter-web、内置的 Tomcat ))