1.最简单方式:参数
例如:
@RequestMapping("/test") @ResponseBody public void saveTest(HttpServletRequest req, HttpServletResponse resp){
}
2.加入监听器,然后再controller里面获取
(1)web.xml相应位置插入
<listener> <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> </listener>
(2)
HttpServletRequest req = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
HttpServletResponse resp = ((ServletWebRequest)RequestContextHolder.getRequestAttributes()).getResponse();
3:使用注解方法注入(该方法未经验证,请读者自行测试)