zoukankan      html  css  js  c++  java
  • 在springMVC的controller中获取request,response对象的一个方法

    https://www.cnblogs.com/yeqingxue/p/11193455.html

    问题描述:

      使用springMVC的时候,有些时候会需要获取请求或者响应对象,例如在身份验证的时候,需要获取请求头中的token,在做登录系统的时候需要使用response对象向客户端添加cookie,一个有效的做法是在controller的方法中添加对应参数如下所示:

    复制代码
    @RestController
    public class Test2Contrller {
        @RequestMapping("/test")
        public void test(HttpServletRequest req, HttpServletResponse res) { 
        // todo
       }
    }
    复制代码

    这样做有一个问题,就是如果这个系统是作为接口并希望被远程调用的,那么额外的参数的存在便会破坏原本的接口定义,造成麻烦,下面介绍两种不需要在方法中增加额外参数就能获取request和response的方式

    第一种方式:通过RequestContextHolder类的方法获取requestAttributes,再从中获取请求和响应对象;

    复制代码
    @RestController
    public class Test2Contrller {
        @RequestMapping("/testreq")
        public void test() {
            // 获得request对象,response对象
            ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            HttpServletRequest request = attributes.getRequest();
            HttpServletResponse response = attributes.getResponse();
            try {
                response.getWriter().write("hello");
            } catch (IOException e) {
                e.printStackTrace();
            }
            Enumeration<String> headerNames = request.getHeaderNames();
            while (headerNames.hasMoreElements()) {
                String name = headerNames.nextElement();
                String value = request.getHeader(name);
                System.out.println(name + "===========" + value);
            }
    
        }
    }
    复制代码

    第二种方式:可以将请求和响应对象抽取出来放在一个超类中,需要使用这两个对象的controller继承这个类,直接使用即可,代码如下:

    超类:

    复制代码
    public class BaseController {
      // 这些对象何以直接被子类使用 protected HttpServletRequest request; protected HttpServletResponse response; protected HttpSession session; @ModelAttribute public void setReqAndRes(HttpServletRequest req, HttpServletResponse res) { this.request = req; this.response = res; this.session = req.getSession(); } }
    复制代码

    子类:

    复制代码
    @RestController
    public class Test3Contrller extends BaseController{
        @RequestMapping("/testreq2")
        public void test() {
            try {
                response.getWriter().write("hello");
            } catch (IOException e) {
                e.printStackTrace();
            }
            Enumeration<String> headerNames = request.getHeaderNames();
            while (headerNames.hasMoreElements()) {
                String name = headerNames.nextElement();
                String value = request.getHeader(name);
                System.out.println(name + "===========" + value);
            }
    
        }
    }
    复制代码

    可以看到第二种方式代码简洁很多,如果对请求或者响应对象有大量的使用需求,例如需要从传过来的请求头中的token获取用户信息,动态的返回结果时,建议使用第二种方式;

  • 相关阅读:
    Dictionaries and lists
    Looping and dictionaries
    Dictionary as a set of counters
    Dictionaries
    List exercise
    Lists and strings
    Copying lists
    Objects and values
    [luoguP1944] 最长括号匹配_NOI导刊2009提高(1)
    [luoguP1868] 饥饿的奶牛(DP)
  • 原文地址:https://www.cnblogs.com/xiang--liu/p/11505849.html
Copyright © 2011-2022 走看看