I want to load a nested List property into a JAVA POJO
I am loading the properties from yml using ConfigurationProperties
annotation over a class A. My List is of type B. This B object has a LIST attribute of it's own. However yml properties are not getting loaded as expected.
@ConfigurationProperties(prefix="prop")
public class A{
List<B> b = new ArrayList<>();
//getters and setters ......
}
public class B{
String user; //This property gets loaded.
List<String> list = new ArrayList<>(); //However this list is still empty
//getters and setters ......
}
My properties in application.yml
looks like below.
prop:
-
user: alpha
list: a,b,c
-
user: beta
list: x,y,z
Well this is not the YAML syntax of a List:
list: a,b,c
That is just one string a,b,c
.
If you wish to use a coma delimited list, you can parse it after you load it. Spring does something similar with some of its own properties, like in this example with RabbitMQ properties. The addresses
are coma delimited, and the function parseAddresses()
splits the string after loading, as part of the setter method of that member.
Otherwise use the YAML list syntax.
prop:
- user: alpha
list:
- a
- b
- c
- user: beta
list:
- x
- y
- z
According to the manual you define your list items as:
list:
- a
- b
- c
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.