SpringBoot全局获取request,response线程安全
环境
SptingBoot: 2.0.+
code
- service
@Service
public class ContextServiceImpl implements ContextService {
/**
* 获取request
*
* @return
*/
@Override
public HttpServletRequest getHttpServletRequest() {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
return request;
}
@Override
public HttpServletResponse getHttpServletResponse() {
HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
return response;
}
}
- 验证