Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mapping List in YAML file to List of objects

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

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.

My YAML file

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

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
  }

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 
   }

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?

like image 977
Omar Avatar asked Sep 11 '25 10:09

Omar


1 Answers

Try this

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

    private List<Bucket> buckets;

  //others methods 
}
like image 123
pvpkiran Avatar answered Sep 14 '25 01:09

pvpkiran