Skip to content
Advertisement

Spring boot read array from YAML (properties) file

This is my project structure

JavaScript

and i have this in application.yml

JavaScript

in my Endpoint i have the following

JavaScript

in any function i can access something like System.out.println(hello) and its perfectly working but for the fileTypes its not even compiling and i receive this error :

Error creating bean with name ‘configurationEndPoint’: Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder ‘document.templates.filetypes’ in value “${document.templates.filetypes}”

Searched alot and every solution i could find was that pointing to the write application.yml/application.xml file, which is invalid in my case since i can read the other test String but not the array;

i tried String[] i tried ArrayList<String> but i can’t get it to work

Advertisement

Answer

The other solution provided by @Jose Martinez would work but not really as clear as needed, because its reading document.templates.filetypes as a String and then splitting it into array of Strings; thus i am adding my solution to this,

1- Create new class FileTypesProperties

JavaScript

2- Create service and inject the previous class

JavaScript

3- In your end point simply autowire and call the previous service

JavaScript

And then your yaml file can be a real array

JavaScript

I believe this is cleaner than splitting a String into smaller strings into an array, hope this will help out someone.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement