本文主要是介绍SpEL解析Map嵌套(字典),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
预备知识参考
玩转Spring中强大的spel表达式!
主要涉及到的内容
集合,字典
元素访问
SpEL目前支持所有集合类型和字典类型的元素访问,使用“集合[索引]”访问集合元素,使用“map[key]”访问字典元素;
//SpEL内联List访问
int result1 = parser.parseExpression("{1,2,3}[0]").getValue(int.class); //SpEL目前支持所有集合类型的访问
Collection<Integer> collection = new HashSet<Integer>();
collection.add(1);
collection.add(2); EvaluationContext context2 = new StandardEvaluationContext();
context2.setVariable("collection", collection);
int result2 = parser.parseExpression("#collection[1]").getValue(context2, int.class); //SpEL对Map字典元素访问的支持
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("a", 1); EvaluationContext context3 = new StandardEvaluationContext();
context3.setVariable("map", map);
int result3 = parser.parseExpression("#map['a']").getValue(context3, int.class);
列表,字典
,数组元素修改
可以使用赋值表达式或Expression接口的setValue方法修改;
@Test
public void test8() {ExpressionParser parser = new SpelExpressionParser();//修改list元素值List<Integer> list = new ArrayList<Integer>();list.add(1);list.add(2);EvaluationContext context1 = new StandardEvaluationContext();context1.setVariable("collection", list);parser.parseExpression("#collection[1]").setValue(context1, 4);int result1 = parser.parseExpression("#collection[1]").getValue(context1, int.class);System.out.println(result1);//修改map元素值Map<String, Integer> map = new HashMap<String, Integer>();map.put("a", 1);EvaluationContext context2 = new StandardEvaluationContext();context2.setVariable("map", map);parser.parseExpression("#map['a']").setValue(context2, 4);Integer result2 = parser.parseExpression("#map['a']").getValue(context2, int.class);System.out.println(result2);
}
我们的需求:要处理Map<String,Object>
划重点:Map的value是Object类型,那可选项就多了,如基本类型、Collection、Map、JavaBean等
自然上边默认的就无法满足了
{"id":888,"name":"zhangsan","age":19,"books":[{"bname":"water book","bauthor":"lian"},{"bname":"water book2","bauthor":"lian2"}]
}
对于Map类型,默认使用map['a']
,而非map.get("a")
这样如果有多层嵌套,那还得一层一层改,如下
["response.body['mappingb']=request.header['token']",
"response.body['mappingb4']=request.body['books'][1]['bname']",
"response.body['mappingb2']=request.query['tag']",
"response.header['test']=request.body['age']",
"response.header['test2']=request.body['name']",
"response.header['test2']=request.header['detail']"
进阶参考
Spring Expression Language (SpEL)
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext evaluationContext = (StandardEvaluationContext) SpelContextFactory.getEvaluationContext(root);
// 划重点
evaluationContext.addPropertyAccessor(new MapAccessor());List<String> expressions = (List<String>) this.getAttribute("expressions");
Iterator<String> it = expressions.iterator();
while (it.hasNext()) {Expression expression = parser.parseExpression(it.next());Object result = expression.getValue(evaluationContext);if (LOGGER.isDebugEnabled()) {LOGGER.debug("expression=" + expression.getExpressionString() + ",result=" + String.valueOf(result));}
}
这篇关于SpEL解析Map嵌套(字典)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!