【发布时间】:2021-11-14 22:42:45
【问题描述】:
ExpressionFactory 必须能够处理以下类型的表达式参数输入:
使用相同分隔符的多个表达式(例如${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(#)'
【问题讨论】:
-
你试过了吗...
-
@M.Deinum 已对其进行了更新
-
你想要达到什么目的?如果要连接两个结果,请在两者之间使用 +。
-
@M.Deinum 感谢添加 + 确实部分解决了我的问题。我想要实现的是使用第一个表达式的输出来评估第二个表达式。例如:-假设您的购物车中有 N 件商品,并且您使用表达式 Item.price 收集所有价格,现在可以在我的下一个表达式中使用将是 json 数组的 out 对它们求和并找到总数我知道我可以在内置函数中使用 sum。但对于其他情况,是否有可能,或者我是否需要在上下文中单独注册/设置?我正在尝试一次评估多个表达式
-
您可以使用流并聚合结果。您可以在其中编写java程序。但为什么要为此使用 SPeL?
标签: java spring-boot spring-el