【问题标题】:Spring Boot 2.0.5 can not bind list of properties from spring-cloud-config serverSpring Boot 2.0.5 无法从 spring-cloud-config 服务器绑定属性列表
【发布时间】:2019-03-26 17:43:54
【问题描述】:

我遇到了下一个情况:我已经从 Spring Boot 1.5.x 迁移了我的应用程序 到 Spring Boot 2.0.5。

我有下一堂课:

@ConfigurationProperties(prefix = "some.property")
public class Myclass {

@Getter
@Setter
private List<String> list;

}

我也有这样的 yml 配置:

some:
  property:
    list:
      - value 1
      - value 2
      - value 3 

此配置是从远程 spring-cloud-config 服务器获取的。

如果我尝试运行应用程序,我会遇到下一个异常:

org.springframework.boot.
context.properties.bind.BindException: Failed to bind properties under 
'some.property' to Myclass


Description:

Property: some.property.list[0]
Value: value 1
Origin: "some.property.list[0]" from property source "bootstrapProperties"
Reason: The elements 
[some.property.list[0],some.property.list[1],some.property.list[2]] were 
left unbound.
   Property: some.property.list[1]
Value: value 2
Origin: "some.property.list[1]" from property source "bootstrapProperties"
Reason: The elements 
[some.property.list[0],some.property.list[1],some.property.list[2]] were 
left unbound.

Property: some.property.list[2]
Value: value 3
Origin: "some.property.list[2]" from property source "bootstrapProperties"
Reason: The elements 
[some.property.list[0],some.property.list[1],some.property.list[2]] were 
left unbound.

但如果我使用本地 bootstrap.yml 文件而不是远程配置服务器 - 一切都很好。

有人遇到过同样的问题吗?我真的需要你的帮助。

附: Spring config server 也有 2.0.5 版本。

【问题讨论】:

  • 您似乎省略了 $ 和花括号 {}。你应该这样声明:@Value("${some.property.list}")
  • Ebert Toribio,我在上面的示例中犯了一个错误,实际上我使用了@ConfigurationProperties(prefix = "some.property")。对此感到抱歉。
  • 你有同样的问题吗?
  • 是的,问题和上面一样。
  • 您可以这样放置您的属性吗:Value1、Value2、Value3。正如我所见,您正在列出值,但您需要在同一行中用逗号分隔它们

标签: spring-boot spring-cloud-config spring-config


【解决方案1】:

我终于找到了问题的根本原因。

好吧,因为我对 Cloud Config Server 不是超级有经验的人,所以很难找到原因。这完全是关于覆盖不同配置文件的属性列表:

假设您的配置服务器上有两个属性文件:

application.yml

application-dev.yml - 它具有更高的优先级,因此它会覆盖之前的所有内容。

在 application.yml 我有这样的属性

some:
  property:
    list:

所以,这只是一个空列表。

但在 application-dev.yml 我有这样的属性:

some:
  property:
    list:
      - value 1
      - value 2
      - value 3

因此,在这种情况下,您将收到我上面提到的错误。 所有你需要做的 - 像这样修复空列表。

some:
  property:
    list:
      - ""

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-01
    • 2018-11-10
    • 1970-01-01
    • 2019-12-25
    • 1970-01-01
    • 2021-03-22
    • 2021-11-11
    • 1970-01-01
    相关资源
    最近更新 更多