YAML to Java List of Objects in Spring Boot

Examples of reading collection of properties from a YAML file or an application properties files into Java List of Objects in Spring Boot.

Overview

This is a quick tutorial to cover examples of reading list or collections from a YAML file or a Properties file as Java List or Set instances in Spring Boot.

We already know, @ConfigurationProperties annotation in Spring Boot maps the YAML or Properties based configurations into Java bean instances and it is very flexible. To learn more about the annotation, we recommend reading Using @ConfigurationProperties in Spring Boot.

Lists in YAML or Properties Files

Let’s have a quick look at the different types of lists that can appear in the a YAML file.

Plain Lists In YAML

property: list: - 1 - 2 - 4 - 6
Code language: YAML (yaml)

The YAML file defines a list of 4 elements. Similar configuration can be expressed through an equivalent properties file instead.

property.list[0]=1 property.list[1]=2 property.list[2]=4 property.list[3]=6
Code language: Properties (properties)

List of Map in YAML

property: listOfMaps: - key1: valueA key2: valueB key3: valueC - key1: valueD key4: valueE

We can clearly see the elements in the list contains random fields and that is why they represent a List of Map.

List of Objects in YAML

property: listOfObjects: - field1: valueA field2: valueB - field1: valueD field2: valueE
Code language: YAML (yaml)

The elements of this YAML configuration list contains exact same fields. Thus, it represents a List of Objects.

In the next sections, we will read such lists into Java Beans using @ConfigurationProperties annotation. Remember that, instead of a YAML file we can use any equivalent properties file and the @ConfigurationProperties annotation will work in both cases.

YAML to Plain Java List

Consider, our YAML file has a List of simple elements, as shown in the next snippet.

config: env: - dev - qa - prod
Code language: YAML (yaml)

In order to read this configuration, we will create a class and use @ConfigurationProperties annotation along with a prefix of config.

@Configuration @ConfigurationProperties(prefix = "config") public class PlainListProperties { private List<String> env; // Constructor, Getter, and Setter methods // toString() }
Code language: Java (java)

We have defined a simple List of String elements having the name in accordance with the name of the YAML list.

* Plain Java List based Properties
env: [dev, qa, prod]

We have printed the populated bean instance using a @PostConstruct method and it shows the list is read correctly.

YAML to Java List of Map

Lists in a YAML (or even a properties file) will not always contain plain elements and they can be more complex. For example list of unrelated group of fields.

config: miscellaneous: - poll-frequency: 20 timeout: 10 max-retry: 3 - log-erros: true fail-on-errors: false - publish-metrics: true metrics-frequency: 30
Code language: YAML (yaml)

We can see that each element in the list has a group of unrelated fields. To read the config as a List<Map>, we will create a configuration properties class and use a prefix.

@Configuration @ConfigurationProperties(prefix = "config") public class ListOfMapProperties { private List<Map<String, Object>> miscellaneous; // Constructor, Getter, and Setter methods // toString() }
Code language: Java (java)

Note that our list expects a Map<String, Object> type of elements, which exactly matches with the config we have.

miscellaneous: 
	{poll-frequency=20, timeout=10, max-retry=3}
	{log-erros=true, fail-on-errors=false}
	{publish-metrics=true, metrics-frequency=30}

Printing the list using a well-formatted toString() method, we see that the list of maps from the YAML configuration is correctly mapped into a Java List of Map elements.

YAML to Java List of Object

Let’s consider our YAML configuration has a list “services“. All the elements of the list have exact same fields. That is why we can map the list as a Java List of objects.

config: services: - name: login-service url: http://login.example.com - name: data-service url: http://data.example.com
Code language: YAML (yaml)

In order to read the configuration, we will create a Java class and annotate it with @ConfigurationProperties and add the required prefix.

@Configuration @ConfigurationProperties(prefix = "config") public class ListOfObjectProperties { private List<<meta charset="utf-8">Service> services; // Constructor, Getter, and Setter methods // toString() public static class Service { private String name; private String url; // Constructor, Getter, and Setter methods } }
Code language: Java (java)

Note, that we are using a nested inner class (Service) to bind properties of each element in the configuration list.

Upon startup, let’s print the bean using a well-formatted toString() method.

* Java List of Object based Properties
services: 
	name: login-service, url: http://login.example.com
	name: data-service, url: http://data.example.com

It shows that the list in the YAML configuration is correctly mapped into the Java List of custom objects.

YAML to Java Set

So far, our examples used Java List based mappings. However, we can also map lists from YAML or Properties configurations into a Java Set instance.

The advantage of using Set over a Java List is that, Set implementations are unique in nature. Thus it will remove all the duplicates from the List.

To demonstrate a simple mapping of YAML list into Java Set we will re-write the previous example using Set.

<meta charset="utf-8">@Configuration @ConfigurationProperties(prefix = "config") public class SetOfObjectProperties { private Set<<meta charset="utf-8">Service> services; // Constructor, Getter, and Setter methods // toString() public static class Service { private String name; private String url; // Constructor, Getter, and Setter methods } }
Code language: Java (java)

Compared to the example from the previous section, the only difference is the type of services variable, which is now a Set.

* Java Set of Object based Properties
services: 
	name: login-service, url: http://login.example.com
	name: data-service, url: http://data.example.com

As expected, the list in the YAML configuration is correctly mapped into a Java Set.

Summary

This quick tutorial illustrated different ways of mapping YAML or Properties configurations into Java List instances. We understood a different forms of YAML or Properties configurations list – plain list, list of maps, and list of objects. Then we created examples using @ConfigurationProperties to map these configurations lists into plain Java List, List of Map, or List of Java Objects.

In the last section, we understood that we can also bind YAML or Properties configurations in Java Set. Doing so, we always get a list of unique elements.

For the full source code of the examples used in this tutorial, you can refer to our Github Repository.