Monday, June 6, 2022

 

How to Define a Map in YAML for a POJO

1. Overview

In this tutorial, we'll walk through how we can use properties defined in a YAML file to configure values for a Map in our POJO classes.

2. POJO and YAML

POJO classes are Plain Old Java Objects. YAML is a human-readable structured data format that uses indentation to indicate nesting.

2.1. Simple Map Example

Let's imagine that we are running an online store, and we are creating a service that translates clothing sizes. At first, we are only selling clothes in the UK. We want to know what UK size the label “S”, “M”, “L” and so on refers to. We create our POJO configuration class:

@ConfigurationProperties(prefix = "t-shirt-size")
public class TshirtSizeConfig {

    private Map<String, Integer> simpleMapping;

    public TshirtSizeConfig(Map<String, Integer> simpleMapping) {
        this.simpleMapping = simpleMapping;
    }
    
    //getters and setters..
}

Notice the @ConfigurationProperties with the prefix value. We'll define our mapping under that same root value in our YAML file, as we can see in the next section.

We also need to remember to enable configuration properties with the following annotation on our Application.class:

@EnableConfigurationProperties(TshirtSizeConfig.class)
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

2.2. YAML Configuration

Now we add t-shirt-size to our YAML configuration.

We can use the following structure in our application.yml file:

t-shirt-size:
  simple-mapping:
    XS: 6
    S:  8
    M:  10
    L:  12
    XL: 14

Notice the indentation and the spaces. YAML uses indentation to indicate nesting. The recommended syntax is two spaces for each nested level.

Notice how we're using simple-mapping with the dash, but our property name in our class is called simpleMapping.  YAML properties with dashes will automatically translate to the camel-case equivalent in code.

2.3. More Complex Map Example

After our successful UK shops, we now need to consider translating sizes to other countries' measurements. For example, we now want to know what size is the label “S” in France and the US. We need to add another layer of data to our configuration.

We can alter our application.yml with a more complex mapping:

t-shirt-size:
  complex-mapping:
    XS:
      uk: 6
      fr: 34
      us: 2
    S:
      uk: 8
      fr: 36
      us: 4
    M:
      uk: 10
      fr: 38
      us: 6
    L:
      uk: 12
      fr: 40
      us: 8
    XL:
      uk: 14
      fr: 42
      us: 10

The corresponding field in our POJO will be a map of maps:

private Map<String, Map<String, Integer>> complexMapping;

3. Conclusion

In this article, we saw how we could define simple and more complex nested maps in a YAML configuration file for a simple POJO.

The code for this article is available over on GitHub

No comments:

Post a Comment

So sánh các GitFlow model và áp dụng với CICD

https://medium.com/oho-software/so-s%C3%A1nh-c%C3%A1c-gitflow-model-v%C3%A0-%C3%A1p-d%E1%BB%A5ng-v%E1%BB%9Bi-cicd-b6581cfc893a