原因:org.springframework.expression.spel.SpelParseException:表达式 [] @31:EL1043E:意外的标记。预期为 'colon(:)' 但为 'rcurly(}

Caused by: org.springframework.expression.spel.SpelParseException: Expression [] @31: EL1043E: Unexpected token. Expected 'colon(:)' but was 'rcurly(}

在发布问题之前,我浏览了如下链接:How to pass a Map<String, String> with application.properties,但这不符合我的要求。

application.properties

soap.all.endpoints={getEmployeeRoles: ['ABC', 'EFG'], getStudentRoles: ['PQR', 'XYZ']} 

我正在使用下面的代码阅读,但它给我错误。

@Value("#{${soap.all.endpoints}}")
private Map<String, List<String>> roles;

错误:

Caused by: org.springframework.beans.factory.BeanExpressionException: Expression parsing failed; nested exception is org.springframework.expression.spel.SpelParseException: Expression [{getEmployeeRoles: 'ABC', 'EFG'}] @31: EL1043E: Unexpected token. Expected 'colon(:)' but was 'rcurly(})'
    at org.springframework.context.expression.StandardBeanExpressionResolver.evaluate(StandardBeanExpressionResolver.java:164) ~[spring-context-5.2.2.RELEASE.jar:5.2.2.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.evaluateBeanDefinitionString(AbstractBeanFactory.java:1565) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1231) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1207) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:640) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
    ... 44 common frames omitted
Caused by: org.springframework.expression.spel.SpelParseException: Expression [{getEmployeeRoles: 'ABC', 'EFG'}] @31: EL1043E: Unexpected token. Expected 'colon(:)' but was 'rcurly(})'
    at org.springframework.expression.spel.standard.InternalSpelExpressionParser.internalException(InternalSpelExpressionParser.java:1041) ~[spring-expression-5.2.2.RELEASE.jar:5.2.2.RELEASE]
    at org.springframework.expression.spel.standard.InternalSpelExpressionParser.eatToken(InternalSpelExpressionParser.java:923) ~[spring-expression-5.2.2.RELEASE.jar:5.2.2.RELEASE]
    at org.springframework.expression.spel.standard.InternalSpelExpressionParser.maybeEatInlineListOrMap(InternalSpelExpressionParser.java:667) ~[spring-expression-5.2.2.RELEASE.jar:5.2.2.RELEASE]
    at org.springframework.expression.spel.standard.InternalSpelExpressionParser.eatStartNode(InternalSpelExpressionParser.java:519) ~[spring-expression-5.2.2.RELEASE.jar:5.2.2.RELEASE]
    at org.springframework.expression.spel.standard.InternalSpelExpressionParser.eatPrimaryExpression(InternalSpelExpressionParser.java:350) ~[spring-expression-5.2.2.RELEASE.jar:5.2.2.RELEASE]
    at org.springframework.expression.spel.standard.InternalSpelExpressionParser.eatUnaryExpression(InternalSpelExpressionParser.java:344) ~[spring-expression-5.2.2.RELEASE.jar:5.2.2.RELEASE]
    at org.springframework.expression.spel.standard.InternalSpelExpressionParser.eatPowerIncDecExpression(InternalSpelExpressionParser.java:303) ~[spring-expression-5.2.2.RELEASE.jar:5.2.2.RELEASE]
    at org.springframework.expression.spel.standard.InternalSpelExpressionParser.eatProductExpression(InternalSpelExpressionParser.java:281) ~[spring-expression-5.2.2.RELEASE.jar:5.2.2.RELEASE]
    at org.springframework.expression.spel.standard.InternalSpelExpressionParser.eatSumExpression(InternalSpelExpressionParser.java:263) ~[spring-expression-5.2.2.RELEASE.jar:5.2.2.RELEASE]
    at org.springframework.expression.spel.standard.InternalSpelExpressionParser.eatRelationalExpression(InternalSpelExpressionParser.java:218) ~[spring-expression-5.2.2.RELEASE.jar:5.2.2.RELEASE]
    at org.springframework.expression.spel.standard.InternalSpelExpressionParser.eatLogicalAndExpression(InternalSpelExpressionParser.java:205) ~[spring-expression-5.2.2.RELEASE.jar:5.2.2.RELEASE]
    at org.springframework.expression.spel.standard.InternalSpelExpressionParser.eatLogicalOrExpression(InternalSpelExpressionParser.java:192) ~[spring-expression-5.2.2.RELEASE.jar:5.2.2.RELEASE]
    at org.springframework.expression.spel.standard.InternalSpelExpressionParser.eatExpression(InternalSpelExpressionParser.java:153) ~[spring-expression-5.2.2.RELEASE.jar:5.2.2.RELEASE]
    at org.springframework.expression.spel.standard.InternalSpelExpressionParser.doParseExpression(InternalSpelExpressionParser.java:131) ~[spring-expression-5.2.2.RELEASE.jar:5.2.2.RELEASE]
    at org.springframework.expression.spel.standard.SpelExpressionParser.doParseExpression(SpelExpressionParser.java:61) ~[spring-expression-5.2.2.RELEASE.jar:5.2.2.RELEASE]
    at org.springframework.expression.spel.standard.SpelExpressionParser.doParseExpression(SpelExpressionParser.java:33) ~[spring-expression-5.2.2.RELEASE.jar:5.2.2.RELEASE]
    at org.springframework.expression.common.TemplateAwareExpressionParser.parseExpressions(TemplateAwareExpressionParser.java:121) ~[spring-expression-5.2.2.RELEASE.jar:5.2.2.RELEASE]
    at org.springframework.expression.common.TemplateAwareExpressionParser.parseTemplate(TemplateAwareExpressionParser.java:62) ~[spring-expression-5.2.2.RELEASE.jar:5.2.2.RELEASE]
    at org.springframework.expression.common.TemplateAwareExpressionParser.parseExpression(TemplateAwareExpressionParser.java:49) ~[spring-expression-5.2.2.RELEASE.jar:5.2.2.RELEASE]
    at org.springframework.context.expression.StandardBeanExpressionResolver.evaluate(StandardBeanExpressionResolver.java:142) ~[spring-context-5.2.2.RELEASE.jar:5.2.2.RELEASE]
    ... 48 common frames omitted

我能够使用

解决

您可以使用类似 SPEL json 的语法在 属性 文件中编写一个简单的地图或列表地图。

simple.map={'KEY1': 'value1', 'KEY2': 'value3', 'KEY3': 'value5'}

map.of.list={\
  'KEY1': {'value1','value2'}, \
  'KEY2': {'value3','value4'}, \
  'KEY3': {'value5'} \
 }

我将 \ 用于多行 属性 以增强可读性

然后,在Java中,你可以像这样使用@Value自动访问和解析它。

@Value("#{${simple.map}}")
Map<String, String> simpleMap;

@Value("#{${map.of.list}}")
Map<String, List<String>> mapOfList;

此处使用 ${simple.map}, @Value 从 属性 文件中获取以下字符串:

"{'KEY1': 'value1', 'KEY2': 'value3', 'KEY3': 'value5'}"

然后,它被评估为内联

@Value("#{{'KEY1': 'value1', 'KEY2': 'value3', 'KEY3': 'value5'}}")

您可以在官方文档中了解更多

我实现了

soap.all.endpoints={\
    getEmployeeRoles: {'ABC', 'EFG'}, \
    getStudentRoles: {'PQR', 'XYZ'} \
}

相同的代码。

@Value("#{${soap.all.endpoints}}")
private Map<String, List<String>> roles;