当前位置:网站首页>SpEL表达式 简单使用
SpEL表达式 简单使用
2022-06-13 09:19:00 【tang_sy】
前言:
{
"data":{
"dataList":[
{
"userName":"name"
}
]
},
"code":"200",
"message":"正常"
}你可能在工作中遇到这样一种情况,在jsonStr,要想获取指定的key值的结果
工作中场景:任务中心微服务-导出,在dubbo泛化调用中,不同的业务线返回的结构可能不同,怎么做到根据不同路径获取数据呢?如上图不是dataList,另一个dubbo返回属性为List,怎么办?是不是想让对方统一下,得到的结果就是别人不会理会你,说我的项目已经上线了,我怎么修改?所以百度看csdn,哈哈,找到一套代码,递归查询,完美!
有一天大佬说,SpEL很简单5行代码解决,你说你不会,要去看看,你各种查资料,最后写出了如下代码
import lombok.experimental.UtilityClass;
import org.springframework.context.expression.EnvironmentAccessor;
import org.springframework.context.expression.MapAccessor;
import org.springframework.expression.Expression;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@UtilityClass
public class Spel {
private static final ConcurrentHashMap<String, Expression> c = new ConcurrentHashMap<>();
@SuppressWarnings("unchecked")
public <T> T exec(final String exp, final Object object) {
final Expression expression = c.computeIfAbsent(exp, k -> new SpelExpressionParser().parseExpression(exp));
final StandardEvaluationContext context = new StandardEvaluationContext(object);
context.addPropertyAccessor(new EnvironmentAccessor());
if (object instanceof Map) {
context.addPropertyAccessor(new MapAccessor());
}
return (T) expression.getValue(context);
}
public static void main(String[] args) {
String el = "student==null?null:student[address]==null?null:student[address][city]";
Map<String, Object> address = new HashMap<>();
address.put("city", "北京");
Map<String, Object> student = new HashMap<>();
student.put("address", address);
Map<String, Object> root = new LinkedHashMap<>();
root.put("student", student);
Object exec1;
Object exec2;
Object exec = exec(el, root);
System.out.println("exec = " + exec);
try {
exec1 = exec("test", root);
System.out.println("exec1 = " + exec1);
} catch (Exception e) {
System.out.println("不存在的key,报错啦");
}
exec2 = exec("[test]", root);
System.out.println("exec2 = " + exec2);
}
}
边栏推荐
猜你喜欢

类的加载概述

Tutorial (5.0) 01 Product introduction and installation * fortiedr * Fortinet network security expert NSE 5

Neo4j - CQL使用

基于微信小程序的图书馆管理系统.rar(论文+源码)

Tutorial (5.0) 04 Fortint cloud services and scripts * fortiedr * Fortinet network security expert NSE 5

Library management system based on wechat applet Rar (thesis + source code)

20211104 why are the traces of similar matrices the same

Solov2 nanny level tutorial (including environment configuration, training your own data set, code logic analysis, etc...) Updating ing

BGP Federation +community

JUC 原子累加器 源码之 LongAdder
随机推荐
strcpy_ S precautions for use. (do not use strcpy_s where memcpy_s can be used)
LeetCode 5289. 公平分发饼干(DFS)
Redis fuzzy query batch deletion
C language: deep understanding of pointers and arrays
C language: Simulated Implementation of library function strcpy
20211006 linear transformation
@Value不生效及extend/implement其他类无法注入bean的手动注入
20211005 Hermite matrix and some properties
Yolov5 model evaluation
LeetCode 6095. 强密码检验器 II
Solov2 nanny level tutorial (including environment configuration, training your own data set, code logic analysis, etc...) Updating ing
HAProxy + Keepalived实现MySQL的高可用负载均衡
20211028 Stabilizability
turtle库显示系统时间
JUC atomic accumulator
LeetCode 6097. 替换字符后匹配(字典)
Module build failed: TypeError: this. getOptions is not a function at Object. stylusLoader
Library management system based on wechat applet Rar (thesis + source code)
20211104 为什么相似矩阵的迹相同
JUC 原子累加器 源码之 LongAdder