通过静态方法获取,你也可以封装一个静态方法出来
@GetMapping(value = "") public String center() { ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes(); HttpServletRequest request = servletRequestAttributes.getRequest(); HttpServletResponse response = servletRequestAttributes.getResponse(); //... }
通过参数直接获取,只要在你的方法上加上参数,Springboot就会帮你绑定,你可以直接使用。如果你的方法有其他参数,把这两个加到后面即可。
@GetMapping(value = "") public String center(HttpServletRequest request,HttpServletResponse response) { //... }
注入到类,这样就不用每个方法都写了
@Autowired private HttpServletRequest request; @Autowired private HttpServletResponse response; @GetMapping(value = "") public String center() { //... }