获取request中取得前端传递过来的数据

第一种:request payload请求方式 (传递的为json数据)

/**
      * @author: tianyong
      * @time: 2019/7/9 11:10
      * @description:POST请求参数request转换为Map
      */
    public static Map getPostRequestParams(HttpServletRequest request){
        StringBuffer json = new StringBuffer();
        String line = null;
        try {
            BufferedReader reader = request.getReader();
            while((line = reader.readLine()) != null) {
                json.append(line);
            }
        }
        catch(Exception e) {
            loggers.error("POST请求参数转换错误!",e);
        }
        return str2Map(json.toString());
    }

第二种:formdata请求方式

/**
      * @author: tianyong
      * @time: 2019/5/31 17:14
      * @description:请求参数request转换为Map
      */
    public static Map getRequestParams(HttpServletRequest request){
        //参数定义
        String paraName = null;
        Map parameters = new HashMap<>();
        //获取请求参数并转换
        Enumeration enu = request.getParameterNames();
        while (enu.hasMoreElements()) {
            paraName = enu.nextElement();
            parameters.put(paraName, request.getParameter(paraName));
        }
        return parameters;
    }

 

你可能感兴趣的:(springMVC)