本文主要讲解如何在springboot下获取request和response。

WebUtil.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/**
 * 获取 HttpServletRequest
 */
public static HttpServletResponse getResponse() {
    return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
}

/**
 * 获取 HttpServletRequest
 * @return request
 */
public static HttpServletRequest getRequest() {
    return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
}