zl程序教程

您现在的位置是:首页 >  后端

当前栏目

java实现request请求参数转map工具类

JAVAMap工具 实现 参数 请求 request
2023-09-11 14:20:19 时间

request请求参数转map工具类

package com.example.mimiprogram.utils;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import javax.servlet.http.HttpServletRequest;
import com.alibaba.fastjson.JSONObject;
import com.example.mimiprogram.exception.CodeInvalidException;
import com.example.mimiprogram.exception.LoginInvalidException;

/**
 * request请求参数转map工具类
 * @author Administrator
 *
 */
public class GetParamToMap {

	/**
	 * 3rd_Session专用 request请求参数转map
	 * @param request
	 * @return
	 * @throws Exception
	 */
	public static Map<String, Object> mapForThird_Session(HttpServletRequest request) throws Exception{
		Map<String, Object> paramMap=new HashMap<String, Object>();
		//get
		paramMap=getMethod(request);
		return paramMap;
	}
	
	/**
	 * request请求参数转map(不需要登录的接口)
	 * @param request
	 * @param redisUtil  redis工具类
	 * @return
	 * @throws Exception
	 */
	public static Map<String, Object> getParameterMap(HttpServletRequest request,RedisUtil redisUtil) throws Exception{
		Map<String, Object> paramMap=new HashMap<String, Object>();
		//get
		paramMap=getMethod(request);
		//向请求添加用户参数  userId和openid
		String third_session = String.valueOf(paramMap.get("third_session"));
		if(Tools.notEmpty(third_session)){
			//从redis存储中取所需数据
			Map<String, String> redisMap = redisUtil.hgetAll(third_session);
			if(redisMap ==null){
				//对应用户小程序页面存储主动丢失或失效,需要重新获取third_session
				throw new CodeInvalidException("third_session在redis中失效!");//401
			}
			String userId = redisMap.get("MY_USERID");
			String openid = redisMap.get("openid");
			paramMap.put("MY_USERID", userId);
			paramMap.put("openid", openid);
		}else{
			throw new CodeInvalidException("third_session为空!");
		}
		return paramMap;
	}
	
	/**
	 * request请求参数转map(需要登录的接口)
	 * @param request
	 * @return
	 * @throws Exception
	 */
	public static  Map<String, Object> getParameterMapWXCode(HttpServletRequest request,RedisUtil redisUtil) throws Exception{
		Map<String, Object> paramMap=new HashMap<String, Object>();
		//get
		paramMap=getMethod(request);
		//向请求添加用户参数  userId和openid
		String third_session = String.valueOf(paramMap.get("third_session"));
		if(Tools.notEmpty(third_session)){
			//从redis存储中取所需数据
			Map<String, String> redisMap = redisUtil.hgetAll(third_session);
			if(redisMap ==null){
				//对应用户小程序页面存储主动丢失或失效,需要重新获取third_session
				throw new CodeInvalidException("third_session在redis中失效!");
			}
			String userId = redisMap.get("MY_USERID");
			if(Tools.isEmpty(userId)){
				//对应redis中数据被清空或者redis中值主动失效,需要页面主动重新获取code换取新的third_session
				throw new LoginInvalidException("userId获取失败");
			}
			String openid = redisMap.get("openid");
			paramMap.put("MY_USERID", userId);
			paramMap.put("openid", openid);
		}else{
			//对应用户小程序页面存储主动丢失或失效,需要重新获取third_session
			throw new CodeInvalidException("third_session为空!");
		}
		return paramMap;
	}

	/**
	 * request请求参数转map
	 * @param request
	 * @return
	 * @throws Exception
	 */
	public static Map<String, Object> mapForCommon(HttpServletRequest request) throws Exception{
		Map<String, Object> paramMap=new HashMap<String, Object>();
		
		//获取提交方法
		String submitMethod = request.getMethod();
		if(submitMethod.equals("GET")){//get
			paramMap=getMethod(request);
		}else{//post
			paramMap=postMethod(request);
		}
		return paramMap;
	}
	
	/**
	 * 处理get请求参数:
	 * get请求主要使用ParameterMap进行获取请求参数集合
	 * @param request
	 * @return
	 */
	public static Map<String, Object> getMethod(HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, String[]> properties = request.getParameterMap();//把请求参数封装到Map<String, String[]>中
		//iterator:迭代器
		Iterator<Entry<String, String[]>> iterator=properties.entrySet().iterator();
		String name ="";
		String value ="";
		//遍历
		while(iterator.hasNext()){
			Entry<String, String[]> entry = iterator.next();
			name=entry.getKey();
			Object valueObject = entry.getValue();
			if(null == valueObject){
				value ="";
			}else if(valueObject instanceof String[]){
				String[] values=(String[]) valueObject;
				for(int i=0; i<values.length; i++){
					value = values[i]+",";
				}
				value = value.substring(0, value.length()-1);//去除最后一个逗号
			}else{
				value = valueObject.toString();//用于请求参数中请求参数名唯一
			}
			resultMap.put(name, value);
		}
		
		return resultMap;
	}
	
	/**
	 * 处理post请求参数:
	 * post请求主要使用字节流进行获取请求参数集合
	 * @param request
	 * @return
	 * @throws IOException 
	 */
	public static Map<String, Object> postMethod(HttpServletRequest request) throws IOException {
		
		byte buffer[] = getRequestPostBytes(request);
		String charEncoding = request.getCharacterEncoding();
		if(charEncoding == null){
			charEncoding = "UTF-8";
		}
		String param = new String(buffer, charEncoding);
		if(Tools.isEmpty(param)){
			return null;
		}
		return (Map<String, Object>) JSONObject.parse(param);
	}

	/**
	 * 获取post请求的byte[]数组
	 * @param request
	 * @return
	 * @throws IOException 
	 */
	private static byte[] getRequestPostBytes(HttpServletRequest request) throws IOException {
		int contentLength = request.getContentLength();
		if(contentLength < 0){
			return null;
		}
		byte buffer[] = new byte[contentLength];
		for(int i=0; i<contentLength;){
			int readlen = request.getInputStream().read(buffer, i, contentLength-i);
			if(readlen == -1){
				break;
			}
		}
		return buffer;
	}
}

GetParamToMap 类中的 RedisUtil 见 :点击此处查看