Omar
Omar

Reputation: 41

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?

Upvotes: 4

Views: 6955

Answers (1)

pvpkiran
pvpkiran

Reputation: 27018

Try this

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

    private List<Bucket> buckets;

  //others methods 
}

Upvotes: 2

Related Questions