SpringBoot获取HttpServletRequest、HttpServletResponse的三种方式

2023-12-16 13:28:33

1、直接在Controller层方法参数上获取

仅仅适用在controller方法上。当Spring接收到HTTP请求时,会寻找一个合适的方法来处理该请求。如果该方法参数上标注了@RequestMapping或@Get、@Post等注解,Spring就会将HttpServletRequest对象注入到该参数中。

@RestController
public class Controller {
    @RequestMapping("/test")
    public String test(HttpServletRequest request,HttpServletResponse response) {
        return "success";
    }
}

2、通过@Autowired注入

适用于所有的bean

@Autowired
private HttpServletRequest request;

@Autowired
private HttpServletResponse response;

@RequestMapping("/test")
public String test() {
    Enumeration<String> headerNames = request.getHeaderNames();
    response.setHeader("header","header_value");
    return "success";
}

通过调试可以看到,注入的Reques是一个代理类,而这个被代理的目标由RequestObjectFactory.getObject() 获取
在这里插入图片描述
我们再看一下RequestObjectFactory.getObject() 的方法,最终还是走到了RequestContextHolder.getRequestAttributes()方法

private static class RequestObjectFactory implements ObjectFactory<ServletRequest>, Serializable {
	@Override
	public ServletRequest getObject() {
		return currentRequestAttributes().getRequest();
	}
	
	@Override
	public String toString() {
		return "Current HttpServletRequest";
	}
}

private static ServletRequestAttributes currentRequestAttributes() {
	RequestAttributes requestAttr = RequestContextHolder.currentRequestAttributes();
	if (!(requestAttr instanceof ServletRequestAttributes)) {
		throw new IllegalStateException("Current request is not a servlet request");
	}
	return (ServletRequestAttributes) requestAttr;
}

3、通过RequestContextHolder获取

适用于所有的方法,spring会通过RequestContextHolder.setRequestAttributes方法将RequestAttributes设置到ThreadLocal中。

ServletRequestAttributes attr = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attr.getRequest();
HttpServletResponse response = attr.getResponse();

文章来源:https://blog.csdn.net/Session_s/article/details/135030876
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。