SpringBoot三种获取Request和Response的方法

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

  • 一、可以封装为静态方法。
  ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
    HttpServletRequest request = servletRequestAttributes.getRequest();
    HttpServletResponse response = servletRequestAttributes.getResponse();
  • 二、controller的方法里面。
@GetMapping(value = "")
public String doSomething(HttpServletRequest request,HttpServletResponse response) {
    //...
}
  • 三、直接注入。
@Autowired
private HttpServletRequest request;
 
@Autowired
private HttpServletResponse response;

转载于:https://my.oschina.net/kunBlog/blog/3042788

你可能感兴趣的:(SpringBoot三种获取Request和Response的方法)