SpringBoot三种获取Request和Response的方法

  • 一、可以封装为静态方法。
 
  1. ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();

  2. HttpServletRequest request = servletRequestAttributes.getRequest();

  3. HttpServletResponse response = servletRequestAttributes.getResponse();

  • 二、controller的方法里面。
 
  1. @GetMapping(value = "")

  2. public String doSomething(HttpServletRequest request,HttpServletResponse response) {

  3. //...

  4. }

  • 三、直接注入。
 
  1. @Autowired

  2. private HttpServletRequest request;

  3.  
  4. @Autowired

  5. private HttpServletResponse response;

 

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

你可能感兴趣的:(java)