当前位置:网站首页>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);

    }
}

原网站

版权声明
本文为[tang_ sy]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/164/202206130919308862.html