4

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?

Zoe
  • 27,060
  • 21
  • 118
  • 148
Omar
  • 41
  • 1
  • 4

1 Answers1

2

Try this

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

    private List<Bucket> buckets;

  //others methods 
}
pvpkiran
  • 25,582
  • 8
  • 87
  • 134
  • 1
    the list (buckets) stays null when I try this. – Omar Jun 13 '18 at 13:09
  • do you have getters ans setters for buckets? – pvpkiran Jun 13 '18 at 13:24
  • In my class Bucket I have getters and setters, but not for the list buckets. – Omar Jun 13 '18 at 13:25
  • 1
    try adding getters and setters for buckets also. It should work – pvpkiran Jun 13 '18 at 13:36
  • Create a constructor in your bucket class that takes each of the member variables. IIRC, Spring will want to use constructor injection here. – Michael Peacock Jun 13 '18 at 13:43
  • @MichaelPeacock not necessary. I just tried out. getter/setters in both Bucket and S3ClientHelper classes are enough – pvpkiran Jun 13 '18 at 13:44
  • @MichaelPeacock I had already a constructor in Bucket which takes each member variables. – Omar Jun 13 '18 at 13:58
  • @pvpkiran I tried what you said, but it's always the same error. I think that my Spring Application has problem to map list in YAML file. Event if I try with simple list of String I got the same error. But I can inject others simple attributes. – Omar Jun 13 '18 at 14:03