Spring中获取HttpServletRequest、Http

2019-07-11  本文已影响0人  YongSiv

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() {
    //...
}
上一篇下一篇

猜你喜欢

热点阅读