【问题标题】:read list of objects from .yml with Spring-Boot 2.1 [duplicate]使用 Spring-Boot 2.1 从 .yml 读取对象列表 [重复]
【发布时间】:2019-08-21 23:40:48
【问题描述】:

我需要创建 @ConfigurationProperties 来读取包含复杂对象列表的 .yml 文件。

Spring-Boot 似乎遇到了一些问题,所以我一步一步尝试先加载字符串列表:

.yml:

qwer: asdf
strings:
    - Apple
    - Orange
    - Strawberry
    - Mango

配置类:

@Component
@PropertySource(value = {"tsp_client.yaml", "file:tsp_client.yaml"}, ignoreResourceNotFound = true)
@ConfigurationProperties
public class TSPClientConfig {
    public String qwer;

    public List<String> strings;

    public String getQwer() {
        return qwer;
    }

    public void setQwer(String qwer) {
        this.qwer = qwer;
    }

    public List<String> getStrings() {
        return strings;
    }

    public void setStrings(List<String> strings) {
        this.strings = strings;
    }
}

有了这个,我仍然得到大小为 0 的列表。qewr 属性映射很好。 字符串上的缩进应该没问题,因为我是从 here 复制的。

那么,你能告诉我是 spring 对此有一些问题还是我在这里做错了什么?最终我需要在列表中有复杂的对象。

Spring-Boot:2.1.2.RELEASE

【问题讨论】:

  • 什么重复?然后链接解决方案
  • 复杂对象的映射在哪里?

标签: java spring-boot yaml configurationproperty


【解决方案1】:

显然杰克逊也有一个 yaml 阅读器: https://dzone.com/articles/read-yaml-in-java-with-jackson

如果它没有列表设置器,你可以这样做

setStrings(String[] ar){
this.strings=Arrays.asList(ar);
}

【讨论】:

  • 拍的不错,但也不好用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-10
  • 2016-01-26
  • 2020-02-14
  • 2015-01-03
  • 2017-09-12
  • 2020-03-26
相关资源
最近更新 更多