无法使用@ConfigurationProperties 读取 yaml 的复杂对象.整数不能转换为字符串 [英] Can't read yaml's complex object using @ConfigurationProperties. Integer cannot be cast to String

查看:80
本文介绍了无法使用@ConfigurationProperties 读取 yaml 的复杂对象.整数不能转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试读取这个 yml 文件

I'm trying to read this yml file

dist-price:
  1234:
    foo: 4567
    bar: false

并放入这个类.(我使用的是 Lombok 和 Spring Boot v1.5.4.RELEASE)

and put into this class. (I'm using Lombok and 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;
}

但是我遇到了这个错误.

But I got this error.

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 更改为 TreeMap 它工作正常.但我真的需要使用Integer.似乎 TreeMap 的键(在本例中:1234)被转换为 String.我不知道为什么.

If I change TreeMap<Integer, Bound> to TreeMap<String, Bound> it works fine. But I really need to use Integer. It seems like TreeMap's key (in this case: 1234) is being cast to String. I don't know why.

TreeMapTreeMap 时很好,yml 是这样的.

It was fine when TreeMap<Integer, Bound> was TreeMap<Integer, Integer>, and yml was like this.

dist-price:
  1234: 4567

我尝试过 Spring Boot v1.5.9.RELEASE,但没有成功.

I tried Spring Boot v1.5.9.RELEASE, but no luck.

推荐答案

我根据问题创建了一个示例项目:https://github.com/izeye/so-48071057

I created a sample project from the question: https://github.com/izeye/so-48071057

并确认从这一行抛出异常:https://github.com/spring-projects/spring-boot/blob/1.5.x/spring-boot/src/main/java/org/springframework/boot/bind/RelaxedDataBinder.java#L328

and confirmed the exception is thrown from this line: https://github.com/spring-projects/spring-boot/blob/1.5.x/spring-boot/src/main/java/org/springframework/boot/bind/RelaxedDataBinder.java#L328

如您所见,key 被键入为 String 并且它是异常的根本原因.我不确定这是否合法,因为我从未想过它的 Integer 键.

As you can see, key is typed as String and it's the root cause of the exception. I'm not sure this is legit usage or not as I never thought about Integer key for it.

这篇关于无法使用@ConfigurationProperties 读取 yaml 的复杂对象.整数不能转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆