无法使用 @ConfigurationProperties 读取 yaml 的复杂对象。整数不能转换为字符串
Can't read yaml's complex object using @ConfigurationProperties. Integer cannot be cast to String
我正在尝试读取这个 yml 文件
dist-price:
1234:
foo: 4567
bar: false
并放入这个class。 (我正在使用 Lombok 和 Spring Boot v1.5。4.RELEASE)
@Repository
@ConfigurationProperties
@Data
@NoArgsConstructor
public class WebConfigProperty {
@NonNull
private TreeMap<Integer, Bound> distPrice;
}
@Data
@NoArgsConstructor
public class Bound {
@NonNull
private Integer foo;
@NonNull
private Boolean bar;
}
但是我遇到了这个错误。
Caused by: java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
at java.lang.String.compareTo(Unknown Source)
at java.util.TreeMap.getEntry(Unknown Source)
at java.util.TreeMap.get(Unknown Source)
at org.springframework.boot.bind.RelaxedDataBinder.isBlanked(RelaxedDataBinder.java:328)
at org.springframework.boot.bind.RelaxedDataBinder.initializePath(RelaxedDataBinder.java:283)
at org.springframework.boot.bind.RelaxedDataBinder.normalizePath(RelaxedDataBinder.java:259)
at org.springframework.boot.bind.RelaxedDataBinder.modifyProperty(RelaxedDataBinder.java:240)
at org.springframework.boot.bind.RelaxedDataBinder.modifyProperties(RelaxedDataBinder.java:155)
at org.springframework.boot.bind.RelaxedDataBinder.doBind(RelaxedDataBinder.java:128)
at org.springframework.validation.DataBinder.bind(DataBinder.java:740)
at org.springframework.boot.bind.PropertiesConfigurationFactory.doBindPropertiesToTarget(PropertiesConfigurationFactory.java:272)
at org.springframework.boot.bind.PropertiesConfigurationFactory.bindPropertiesToTarget(PropertiesConfigurationFactory.java:240)
at org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor.postProcessBeforeInitialization(ConfigurationPropertiesBindingPostProcessor.java:329)
... 73 common frames omitted
如果我将 TreeMap<Integer, Bound>
更改为 TreeMap<String, Bound>
,它可以正常工作。但我真的需要使用Integer
。似乎 TreeMap
的密钥(在本例中为:1234
)正在转换为 String
。不知道为什么。
TreeMap<Integer, Bound>
的时候还好TreeMap<Integer, Integer>
,yml是这样的
dist-price:
1234: 4567
编辑: 我试过 Spring 启动 v1.5.9.RELEASE
,但没有成功。
这与 @ConfigurationProperties
无关,而是 YAML 处理键的方式。 YAML 中的键默认是 String
所以如果你真的想在那里使用整数你必须以某种方式强制它。
有几种记录的方法可以做到这一点,引用应该有效但对我不起作用('1234' 给了我 234!)。使用 !!int
强制整数有效
dist-price:
!!int 1234:
foo: 4567
bar: false
我根据以下问题创建了一个示例项目:https://github.com/izeye/so-48071057
如您所见,key
被键入为 String
,这是异常的根本原因。我不确定这是不是合法用法,因为我从未想过 Integer
键。
我正在尝试读取这个 yml 文件
dist-price:
1234:
foo: 4567
bar: false
并放入这个class。 (我正在使用 Lombok 和 Spring Boot v1.5。4.RELEASE)
@Repository
@ConfigurationProperties
@Data
@NoArgsConstructor
public class WebConfigProperty {
@NonNull
private TreeMap<Integer, Bound> distPrice;
}
@Data
@NoArgsConstructor
public class Bound {
@NonNull
private Integer foo;
@NonNull
private Boolean bar;
}
但是我遇到了这个错误。
Caused by: java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
at java.lang.String.compareTo(Unknown Source)
at java.util.TreeMap.getEntry(Unknown Source)
at java.util.TreeMap.get(Unknown Source)
at org.springframework.boot.bind.RelaxedDataBinder.isBlanked(RelaxedDataBinder.java:328)
at org.springframework.boot.bind.RelaxedDataBinder.initializePath(RelaxedDataBinder.java:283)
at org.springframework.boot.bind.RelaxedDataBinder.normalizePath(RelaxedDataBinder.java:259)
at org.springframework.boot.bind.RelaxedDataBinder.modifyProperty(RelaxedDataBinder.java:240)
at org.springframework.boot.bind.RelaxedDataBinder.modifyProperties(RelaxedDataBinder.java:155)
at org.springframework.boot.bind.RelaxedDataBinder.doBind(RelaxedDataBinder.java:128)
at org.springframework.validation.DataBinder.bind(DataBinder.java:740)
at org.springframework.boot.bind.PropertiesConfigurationFactory.doBindPropertiesToTarget(PropertiesConfigurationFactory.java:272)
at org.springframework.boot.bind.PropertiesConfigurationFactory.bindPropertiesToTarget(PropertiesConfigurationFactory.java:240)
at org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor.postProcessBeforeInitialization(ConfigurationPropertiesBindingPostProcessor.java:329)
... 73 common frames omitted
如果我将 TreeMap<Integer, Bound>
更改为 TreeMap<String, Bound>
,它可以正常工作。但我真的需要使用Integer
。似乎 TreeMap
的密钥(在本例中为:1234
)正在转换为 String
。不知道为什么。
TreeMap<Integer, Bound>
的时候还好TreeMap<Integer, Integer>
,yml是这样的
dist-price:
1234: 4567
编辑: 我试过 Spring 启动 v1.5.9.RELEASE
,但没有成功。
这与 @ConfigurationProperties
无关,而是 YAML 处理键的方式。 YAML 中的键默认是 String
所以如果你真的想在那里使用整数你必须以某种方式强制它。
有几种记录的方法可以做到这一点,引用应该有效但对我不起作用('1234' 给了我 234!)。使用 !!int
强制整数有效
dist-price:
!!int 1234:
foo: 4567
bar: false
我根据以下问题创建了一个示例项目:https://github.com/izeye/so-48071057
如您所见,key
被键入为 String
,这是异常的根本原因。我不确定这是不是合法用法,因为我从未想过 Integer
键。