java实现request请求参数转map工具类
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 见 :点击此处查看
相关文章
- 【Java】java扩展机制SPI 实现
- java:Map借口及其子类HashMap五,identityHashMap子类
- JAVA学习(一):Java介绍及其平台、开发环境的配置与搭建
- Java实现 LeetCode 341 扁平化嵌套列表迭代器
- Java中map.getOrDefault()方法的使用
- java实现数组转置
- java实现第三届蓝桥杯方块填数
- Java实现夺冠概率模拟
- Java中List,Set,Map的区别以及API的使用
- Java实现蓝桥杯 最短路
- 【JAVA】 02-Java对象细节
- macos:安装java 17.0.6(android studio报错:Unable to locate a Java Runtime.)
- Eclipse 报 “Exception in thread "main" java.lang.OutOfMemoryError: Java heap space ”错误的解决办法
- Atitit. 获取cpu占有率的 java c# .net php node.js的实现
- Atitit. 数据约束 校验 原理理论与 架构设计 理念模式java php c#.net js javascript mysql oracle
- 华为OD机试 - 免单统计(Java & JS & Python)
- Spire.Doc for Java v11.1.1 Patch
- Java代码规范
- 【java】Java 包(package)
- [手游项目3]-9-Go语言sync.Map(在并发环境中使用的map)
- js 动态设置键值对数组 ,类似于 java 的Map 类型
- Java通过PDF模板导出数据 adobe acrobat的PDF编辑器 itextpdf java导出文件输出流
- 【Android 内存优化】Java 引用类型 ( 强引用 | 软引用 | 弱引用 | 虚引用 )
- Bean-Query 一个把对象转换为Map的Java工具库
- java匿名内部类
- Java Instrumentation 内存马——主要是利用Instrumentation Java API来做内存注入,会用到反射机制,文中提到检测思路:注入jar包-> dump已加载class字节码->反编译成java代码-> 源码webshell检测
- java - 详解 Java 17 中新推出的密封类
- Java Stream 处理分组后取每组最大&Stream流之list转map、分组取每组第一条&Java 8 Collectors:reducing 示例(List分组取最值)
- Java 8 Stream Api 中的 peek、map、foreach区别
- C/C++调用java---JNI常用函数