Java 8 Spring表达式语言无法解析类上的方法
我正在尝试以编程方式计算一些用SpEL编写的表达式Java 8 Spring表达式语言无法解析类上的方法,java-8,spring-el,Java 8,Spring El,我正在尝试以编程方式计算一些用SpEL编写的表达式 ExpressionParser expressionParser = new SpelExpressionParser(); Expression expression = expressionParser.parseExpression(annotation.filter()); SimpleEvaluationContext context = SimpleEvaluationContext.forReadOnlyDataBinding
ExpressionParser expressionParser = new SpelExpressionParser();
Expression expression = expressionParser.parseExpression(annotation.filter());
SimpleEvaluationContext context = SimpleEvaluationContext.forReadOnlyDataBinding().build();
context.setVariable("authorities", authentication.getAuthorities());
return expression.getValue(context, Boolean.class);
如您所见,我添加了一个变量,名为authorities
。我试图计算这个表达式:#authorities.isEmpty()
但我有个例外:
Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1004E: Method call: Method isEmpty() cannot be found on type java.util.ArrayList
at org.springframework.expression.spel.ast.MethodReference.findAccessorForMethod(MethodReference.java:225) ~[spring-expression-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.expression.spel.ast.MethodReference.getValueInternal(MethodReference.java:134) ~[spring-expression-5.1.2.RELEASE.jar:5.1.2.RELEASE]
...
有人知道这个问题的解决方案吗?SimpleEvaluationContext不允许任意方法调用。查看它的Javadocs 如果您“信任”SpEL表达式,请使用
StandardEvaluationContext context = new StandardEvaluationContext();