ExpressionFactory必须能够处理expression参数的以下类型的输入:
使用相同分隔符(例如${employee.firstName}${employee.lastName}
或#{employee.firstName}#{employee.lastName}
)的多个表达式。
我们可以用类似的方法在java SPEL中计算多个表达式吗?
我尝试了以下几种方法
Expression expressionMulti = parser.parseExpression( "#{#jsonPath(#jsonDataObject,'$.customData.price')}#{#jsonPath(#jsonDataObject,'$.previousResponse')}");
获取错误
Exception in thread "main" org.springframework.expression.spel.SpelParseException: Expression [#{#jsonPath(#jsonDataObject,'$.customData.price')}#{#jsonPath(#jsonDataObject,'$.previousResponse')}] @1: EL1043E: Unexpected token. Expected 'identifier' but was 'lcurly({)'
我也尝试过不使用分隔符。
Expression expressionMulti = parser.parseExpression( "#jsonPath(#jsonDataObject,'$.customData.price')#jsonPath(#jsonDataObject,'$.previousResponse')");
Exception in thread "main" org.springframework.expression.spel.SpelParseException: EL1041E: After parsing a valid expression, there is still more data in the expression: 'hash(#)'
发布于 2021-09-21 14:13:56
SpEL只不过是常规Java的语法糖而已。因此,您需要开始考虑它的定义,就像使用常规Java代码一样。因此,请考虑将表达式重写为Java代码,然后逐段转到SpEL。
另一方面,如果您的逻辑如此复杂,请考虑提取一些static
实用程序方法,这些方法可以通过T
运算符https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#expressions-types从SpEL中使用。
相同的#jsonPath()
是JsonPathUtils.evaluate()
之上的SpEL函数,您可以直接在您自己的static
方法中使用它。该实现确实可以使用SpelExpressionParser
来计算这些嵌套的动态表达式。顶部的表达式将使用这些参数调用您的静态方法。
https://stackoverflow.com/questions/69267717
复制相似问题