Search code examples
springspring-el

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


I went through links like: How to pass a Map<String, String> with application.properties before posting the questions, but this doesn't fulfill my requirements.

application.properties

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

I am reading using below code, but it gives me error.

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

Error :

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

Solution

  • I was able to solve using

    You can use the SPEL json-like syntax to write a simple map or a map of list in property file.

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

    I used \ for multiline property to enhance readability

    Then, in Java, you can access and parse it automatically with @Value like this.

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

    Here with ${simple.map}, @Value gets the following String from the property file:

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

    Then, it is evaluated as if it was inlined

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

    You can learn more in the official documentation

    I implemented

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

    Code for the same.

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