YAML to Java List of Objects in Spring Boot

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

Overview

This quick tutorial covers examples of reading lists or collections from a YAML file or a Properties file into Java List or Set instances in a Spring Boot application.

The @ConfigurationProperties annotation in Spring Boot maps the YAML or Properties-based configurations, including collections, into Java bean instances and is very flexible. We recommend reading Using @ConfigurationProperties in Spring Boot to learn more about the annotation.

Lists in YAML or Properties Files

The YAML and Properties files help store and transfer data and data structures, and we mainly use them to keep our Application Configurations. They both allow human-readable syntax for storing different types of data structures like key and value pairs, maps, simple collections, and collections of other objects.

Before we begin, let’s look at the different types of collections or lists that can appear in the YAML file. The YAML

Simple Collections in YAML

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

The YAML file defines a list of 4 elements. We. can also specify a similar configuration in a Properties file instead.

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

Collection of Maps in YAML

Here is a YAML list of Maps.

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

Each element of the ‘listOfMaps‘ collection is a Map, a collection of key and value pairs.

Collection of Objects in YAML

Alternatively, a YAML collection may contain objects.

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

The elements of this YAML collection contain the other fields. Thus, it represents a List of Objects.

This section summarised some simple and complex application configurations we can put in Spring Boot’s application YAML or Properties file. The following sections will practically show how to use Spring’s @PropertiesConfiguration to map the YAML or Properties Collections into Java Collections like List, Set, Map, etc.

YAML to Plain Java List

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

config:
  env:
    - dev
    - qa
    - prodCode language: YAML (yaml)

To map this YAML collection, we will use the @ConfigurationProperties annotation with a prefix of “config“. Because of the prefix, Spring will only map the configurations nested within the “config” element.

@Configuration
@ConfigurationProperties(prefix = "config")
public class PlainListProperties {
  private List<String> env;

  // Constructor, Getter, and Setter methods
  // toString()
}Code language: Java (java)

Here, we used an object of Java List of Strings with the same name as the properties root in the YAML or Properties file. As we read the configurations nested within the “config“, our variable name is “env“.

* Plain Java List based Properties
env: [dev, qa, prod]Code language: plaintext (plaintext)

Starting the application and printing the variable on the startup shows that we correctly mapped the YAML collection into a Java Collection.

YAML to Java List of Map

As we saw earlier, elements of a collection in YAML or Properties files can be a group of unrelated fields. For example, the Collection in the following YAML file contains random fields. We can read such YAML collections into a collection of Java Maps.

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

To do so, we will create an object of List of Map type in our @ConfigurationProperties class.

@Configuration
@ConfigurationProperties(prefix = "config")
public class ListOfMapProperties {
  private List<Map<String, Object>> miscellaneous;

  // Constructor, Getter, and Setter methods
  // toString()
}Code language: Java (java)

Launching the application and printing the miscellaneous object on the startup shows that we correctly read the Collection of maps from the YAML file.

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

YAML to Java List of Object

Alternatively, the collection elements in a YAML or Properties file can have a fixed set of fields, making it a collection of the same object instances. For example, the Collection in the following YAML has two elements containing the same fields representing a list of different services.

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

To map this Collection in Java objects, we will first create a class, Service, having the name and url fields. In the @ConfigurationProperties class, we will define a member of the Collection of the service type.

@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)

Printing the Collection upon startup correctly shows that the Collection of the objects is correctly injected in the Java objects.

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

YAML to Java Set

We mapped the collections from YAML or Properties files in previous examples into Java List instances. However, we can also map them into other collection objects in Java, for example, a Set.

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

Example of Mapping a collection of objects from YAML into a Java Set of objects

<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
Code language: plaintext (plaintext)

As expected, we have correctly mapped the List in the YAML into a Java Set instance.

Summary

This quick tutorial illustrated different ways of mapping YAML or Properties configurations into Java List or Set instances. We understood various forms of YAML or Properties configurations and collections of configurations.

Then we explored examples using @ConfigurationProperties to map these configuration lists into plain Java List, List of Map, or List of Java Objects. In the last section, we understood that we could also bind YAML or Properties configurations in Java Set. By doing so, we always get a list of unique elements.

You can refer to our GitHub Repository for the complete source code of the examples used in this tutorial.

More like this: