当前位置:网站首页>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);
}
}
边栏推荐
- 20211005 Hermite matrix and some properties
- C language: deep understanding of character functions and string functions (2)
- C language: five custom types
- Three indexes reflecting system reliability in performance test: MTTF, MTTR and MTBF
- A static variable is associated with a class and can be used as long as the class is in memory (the variable does not exist as long as your application terminates). (heap body, stack reference)
- 20211104 为什么矩阵的迹等于特征值之和,为什么矩阵的行列式等于特征值之积
- JS【中高级】部分的知识点我帮你们总结好了
- turtle库显示系统时间
- JUC 字段更新器
- @Value不生效及extend/implement其他类无法注入bean的手动注入
猜你喜欢
Library management system based on wechat applet Rar (thesis + source code)
20220524 how to install coppeliasim to disk D
SQL ROW_ The number() function uses
20220606 Young's inequality for Matrices
Redis fuzzy query batch deletion
Online debugging tool Arthas advanced
Simulink variant model and variant subsystem usage
攻防世界PWN play 条件竞争漏洞的利用
final 原理
BGP Federation +community
随机推荐
20211020 academician all drive system
C language: Address Book
JUC原子引用与ABA问题
BGP 联邦+Community
C language: timer principle
LeetCode 6098. 统计得分小于 K 的子数组数目(前缀和+二分查找)
Tutorial (5.0) 03 Security policy * fortiedr * Fortinet network security expert NSE 5
Can the operation of the new BMW I3 meet the expectations of the famous products of the 3 series?
Class loading overview
Tutorial (5.0) 02 Management * fortiedr * Fortinet network security expert NSE 5
阿里高级专家剖析 | Protocol的标准设计
QObject::connect: Cannot queue arguments of type ‘QTextCursor‘ (Make sure ‘QTextCursor‘ is registere
C language: data storage in memory
202012 CCF test questions
[implementation of depth first search]
HAProxy + Keepalived实现MySQL的高可用负载均衡
ROS2之OpenCV人脸识别foxy~galactic~humble
JUC 字段更新器
Overview of common layers of image recognition neural network (under update)
C language: minesweeping