项目地址:easy-plat: 学习spring core ,netty开发高性能框架 (gitee.com)
获取参数名称的一种方式参数映射:
String[] parameterNames =new LocalVariableTableParameterNameDiscoverer().getParameterNames(method);
反射映射参数:
/*** 构造一个请求参数对象*/
public class RequestParam {//是否是参数体private boolean isBody;//参数体,一个参数名独占一行private List
解析原始参数(解决其他版本无法获取报文体参数):
解决这篇文章无法获取POST报文体参数:(1条消息) Netty4 HTTP请求参数解析(GET, POST)_Mrs陶的博客-CSDN博客_netty获取http参数
/*** HTTP请求参数解析器, 支持GET, POST*/
public class RequestParser {private FullHttpRequest fullReq;/*** 构造一个解析器** &#64;param req*/public RequestParser(FullHttpRequest req) {this.fullReq &#61; req;}/*** 解析请求参数** &#64;return 包含所有请求参数的键值对, 如果没有参数, 则返回空Map* &#64;throws RuntimeException* &#64;throws IOException*/public RequestParam parse() throws Exception, IOException {HttpMethod method &#61; fullReq.method();RequestParam param &#61; new RequestParam();Map parmMap &#61; new HashMap<>();List> paramList &#61; new ArrayList<>();if (HttpMethod.GET &#61;&#61; method) {param.setBody(false);// 是GET请求QueryStringDecoder decoder &#61; new QueryStringDecoder(fullReq.uri());decoder.parameters().entrySet().forEach(entry -> {// entry.getValue()是一个List, 只取第一个元素parmMap.put(entry.getKey(), entry.getValue().get(0));paramList.add(parmMap);});} else if (HttpMethod.POST &#61;&#61; method) {// 是POST请求String content &#61; fullReq.content().toString(Charset.defaultCharset());JSONObject jsonObject &#61; JSONUtil.parseObj(content);jsonObject.forEach((key, value) -> {parmMap.put(key, value);});param.setBody(true);paramList.add(parmMap);} else {// 不支持其它方法throw new RuntimeException(""); // 这是个自定义的异常, 可删掉这一行}param.setData(paramList);return param;}
}
将参数映射成反射所需要的Object[]数组参数&#xff1a;
/*** 参数映射*/public static Object[] mappingArgs(Method method, RequestParam requestParam) {// 判断params不为空Class>[] parameterTypes &#61; method.getParameterTypes();Object[] args &#61; new Object[parameterTypes.length];//是否是json报文体String[] parameterNames &#61; new LocalVariableTableParameterNameDiscoverer().getParameterNames(method);if (requestParam.isBody()) {Map param &#61; requestParam.getData().get(0);List list &#61; new ArrayList<>();//参数中多个对象映射param.forEach((k, v) -> {ParamVo paramVo &#61; new ParamVo();paramVo.setName(k);paramVo.setValue(v);list.add(paramVo);});Class>[] paramTypes &#61; method.getParameterTypes();for (int i &#61; 0; i paramType &#61; paramTypes[i];Object value &#61; list.get(i1).getValue();String jsonStr &#61; JSONUtil.toJsonStr(value.toString());// 将json参数映射成对象System.out.println(paramType);//判断paramType是否为字符串类型Object obj &#61; null;if (paramType &#61;&#61; String.class || paramType &#61;&#61; Integer.class || paramType &#61;&#61; Long.class || paramType &#61;&#61; Double.class || paramType &#61;&#61; Float.class || paramType &#61;&#61; Boolean.class) {obj &#61; jsonStr;} else {if (paramType&#61;&#61; Date.class){obj &#61; jsonStr;}else if (paramType&#61;&#61; List.class){obj &#61; JSONUtil.toList(jsonStr, paramType);}else if (paramType&#61;&#61; Map.class){obj &#61; JSONUtil.toBean(jsonStr, paramType);}else {obj &#61; JSONUtil.toBean(jsonStr, paramType);}}args[i] &#61; obj;}}}} else {List> data &#61; requestParam.getData();for (int i &#61; 0; i map &#61; data.get(j);if (map.containsKey(parameterNames[i])) {Object o &#61; map.get(parameterName);args[i] &#61; o;}}}}return args;}
参数包装类&#xff1a;
public class ParamVo {private String name;private Object value;public Object getValue() {return value;}public void setValue(Object value) {this.value &#61; value;}public String getName() {return name;}public void setName(String name) {this.name &#61; name;}
}
执行反射方法&#xff1a;
String content &#61; (String) method.invoke&#xff08;object, args);