问题
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:
put
@EnableConfigurationProperties(SubRegionConfig.class)
to one of your spring configuration class.Create config class:
@ConfigurationProperties(prefix = "sub.region") public static class SubRegionConfig { private Map<String, SubRegion> data; //getters and setters }
Use
.yml
instead of.properties
, like that:sub: region: data: AF: subRegionCd: '34' subRegionName: 'Southern Asia' subRegionDesc: '' subRegionStatus: 'A'
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