Spring中获取参数一般使用@RequestParam、@RequestBody、@PathVariable、@Param等,但是有时候还是需要使用HttpServletRequest、HttpRequestResponse对象。
- 方式一:
@RequestMapping(value = "/test")
public String test() {
ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
HttpServletRequest request = servletRequestAttributes.getRequest();
HttpServletResponse response = servletRequestAttributes.getResponse();
//...
}
- 方式二:
@RquestMapping(value = "/test")
public String test(HttpServletRequest request,HttpServletResponse response) {
//...
}
- 方式三:
@Autowired
private HttpServletRequest httpServletRequest;
@Autowired
private HttpServletResponse httpServletResponse;
@GetMapping(value = "/test")
public String test() {
//...
}
网友评论