Spring SpEL - Expression Language to create Map of String and Custom Object

喜欢而已 提交于 2020-04-07 01:06:35

问题


I'm using Spring Boot example to read the following from the properties file.

sub.region.data={\
    AF: {'subRegionCd' : '34', 'subRegionName' : 'Southern Asia', 'subRegionDesc': '', 'status' : 'A'} \
}

I used below, but it doesn't works

@Value("#{${sub.region.data}}")
private Map<String, SubRegion> subRegionsMap; 

SubRegion.java

public class SubRegion {
    private String subRegionCd;
    private String subRegionName;
    private String subRegionDesc;
    private String subRegionStatus;
}

I am getting below error

Caused by: org.springframework.beans.ConversionNotSupportedException: Failed to convert value of type 'java.util.Collections$UnmodifiableMap' to required type 'java.util.Map'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.util.Collections$UnmodifiableMap' to required type 'com.xxxxxx.model.SubRegion': no matching editors or conversion strategy found
    at org.springframework.beans.TypeConverterSupport.convertIfNecessary(TypeConverterSupport.java:76) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1195) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1167) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:593) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    ... 54 common frames omitted
Caused by: java.lang.IllegalStateException: Cannot convert value of type 'java.util.Collections$UnmodifiableMap' to required type 'com.xxxxxx.model.SubRegion': no matching editors or conversion strategy found
    at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:262) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.beans.TypeConverterDelegate.convertToTypedMap(TypeConverterDelegate.java:608) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:182) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.beans.TypeConverterSupport.convertIfNecessary(TypeConverterSupport.java:73) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    ... 57 common frames omitted

回答1:


looks like you made a mistake after 'subRegionDesc',, i think you mean using colon, not a comma here

With spring boot i suggest you to use ConfigurationProperties, instead of @Value. For example, in this case you have to:

  1. put @EnableConfigurationProperties(SubRegionConfig.class) to one of your spring configuration class.

  2. Create config class:

    @ConfigurationProperties(prefix = "sub.region")
    public static class SubRegionConfig {
        private Map<String, SubRegion> data;
        //getters and setters
    } 
    
  3. Use .yml instead of .properties, like that:

    sub:
      region:
       data:
         AF:
          subRegionCd: '34'
          subRegionName: 'Southern Asia'
          subRegionDesc: ''
          subRegionStatus: 'A'
    
  4. After that you can get every proprties you want from SubRegionConfing

    @Autowired
    private SubRegionConfig subRegionConfig;
    

ConfigurationsProperties is more complex, but more flexible and prefferable to use in most cases.



来源:https://stackoverflow.com/questions/60899860/spring-spel-expression-language-to-create-map-of-string-and-custom-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!