美文网首页
Spring中获取HttpServletRequest、Http

Spring中获取HttpServletRequest、Http

作者: YongSiv | 来源:发表于2019-07-11 18:02 被阅读0次

    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() {
        //...
    }
    

    相关文章

      网友评论

          本文标题:Spring中获取HttpServletRequest、Http

          本文链接:https://www.haomeiwen.com/subject/ydidkctx.html