当前位置:网站首页>Simple use of spiel expressions
Simple use of spiel expressions
2022-06-13 09:24:00 【tang_ sy】
Preface :
{
"data":{
"dataList":[
{
"userName":"name"
}
]
},
"code":"200",
"message":" normal "
}
You may encounter such a situation in your work , stay jsonStr, To get the specified key The result of the value
Work scene : Task center microservices - export , stay dubbo In generalization call , Different business lines may return different structures , How to obtain data according to different paths ? The picture above is not dataList, the other one dubbo The return property is List, What do I do ? Do you want the other party to unify , The result is that people won't pay attention to you , Said that my project has been launched , How can I modify ? So Baidu looks csdn, ha-ha , Find a set of code , recursive query , perfect !
One day the boss said ,SpEL It's simple 5 Line of code to solve , You said you wouldn't , I'm going to have a look , You can check all kinds of information , Finally, the following code is written
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", " Beijing ");
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(" There is no the key, Wrong report ");
}
exec2 = exec("[test]", root);
System.out.println("exec2 = " + exec2);
}
}
边栏推荐
- 图数据库Neo4j介绍
- 20211115 any n-order square matrix is similar to triangular matrix (upper triangle or lower triangle)
- JUC原子整数
- Timestamp to localdate
- C language: summary of question brushing (1)
- @Value does not take effect and extend/implement other classes cannot inject beans manually
- Yolov5 model evaluation
- Jenkins integrates LDAP. The problem of login failure of Jenkins users caused by LDAP configuration error is solved
- Attack and defense world PWN shell
- Heap
猜你喜欢
final 原理
BGP Federation +community
Solov2 source code analysis
Can the operation of the new BMW I3 meet the expectations of the famous products of the 3 series?
Neo4j - CQL use
Simulink variant model and variant subsystem usage
C language: file operation
The turtle library displays the system time
Heap
线上调试工具Arthas基础
随机推荐
Neo4j - CQL use
Neo4j environment construction
Final principle
C language: deep understanding of pointers and arrays
Redis fuzzy query batch deletion
删除软链接
C/s model and P2P model
HAProxy + Keepalived实现MySQL的高可用负载均衡
IP address introduction
C language: preprocessing in program environment
Library management system based on wechat applet Rar (thesis + source code)
20211115 any n-order square matrix is similar to triangular matrix (upper triangle or lower triangle)
Dynamic display of analog clock using digital clock in turtle Library
Yolov5 face learning notes
Tutorial (5.0) 04 Fortint cloud services and scripts * fortiedr * Fortinet network security expert NSE 5
final 原理
@Value does not take effect and extend/implement other classes cannot inject beans manually
LeetCode 583. 两个字符串的删除操作
Subspace of 20211004 matrix
I have summarized the knowledge points of JS [intermediate and advanced] for you