将 YAML 文件中的列表映射到对象列表 [英] Mapping List in YAML file to List of objects

查看:25
本文介绍了将 YAML 文件中的列表映射到对象列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 application.yml 文件中的列表注入 Spring Boot 应用程序中的 Java 对象列表.

I'm trying to inject list in my application.yml file to list of objects Java in my Spring Boot application.

我看过其他类似问题的一些答案将 Yaml 中的列表映射到 Spring Boot 中的对象列表 但我有不同的输出错误.

I have seen some answers to others similar questions Mapping list in Yaml to list of objects in Spring Boot but I have differents output errors.

我的 YAML 文件

s3tool:
   buckets:
     -
      name: myentity1
      accessKey: access
      secretKey: secret
      endpoint: endepoint
      proxyPort: 3128
      proxyHost: gateway-address
     -
      name: myentity2
      accessKey: access
      secretKey: secret
      endpoint: endepoint
      proxyPort: 3128
      proxyHost: gateway-address

我也创建了 Bucket 类

I have also created Bucket class

 public class Bucket {

       private String name;

       private String accessKey;

       private String secretKey;

       private String endpoint;

       private String proxyHost;

       private int proxyPort;

      //getters and setters
  }

以及我在 YAML 中注入列表的配置类

And my configuration class where I inject the list in YAML

    @Configuration
    @EnableConfigurationProperties
    @ConfigurationProperties
    public class S3ClientHelper {

        @Value("${s3tool.buckets}")
        private List<Bucket> buckets;

   //others methods 
   }

当 Spring Boot 开始执行时,我收到以下错误:

When Spring Boot starts excuting I got the following error:

  Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 's3tool.buckets' in value "${s3tool.buckets}"

我也尝试过使用简单的字符串列表,但我也遇到了类似的错误.我该怎么做?

I have also tried with simple list of String but I also got the similar error. How can I do it?

推荐答案

试试这个

@Configuration
@EnableConfigurationProperties
@ConfigurationProperties(prefix = "s3tool")
public class S3ClientHelper {

    private List<Bucket> buckets;

  //others methods 
}

这篇关于将 YAML 文件中的列表映射到对象列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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