Java 获取Request,Response对象方法
第一种、参数
@RequestMapping("/test")
@ResponseBody
public void saveTest(HttpServletRequest req, HttpServletResponse resp){
}
第二种、注解
@Autowired
private HttpServletRequest request;
第三种、上下文获取
1、在web.xml配置监听器
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
2、程序使用(比较重要,能够凭空获取)
HttpServletRequest req = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
HttpServletResponse resp = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getResp