Java Top

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:


Authors Top

If you have a few years of experience in the Java ecosystem, and you’d like to share that with the community, have a look at our Contribution Guidelines.

Lightrun – Third Party Code
announcement - icon

Flakiness in REST requests is a common issue. A request can get a 200 OK in one scenario and a 409 next time. Sometimes a request can even succeed and fail intermittently on the same exact request. In short, working over HTTP can be a bit of a mess without solid tooling.

Also, while it’s easy enough to debug these issues locally when developing the application, we’re talking about production here - we can’t afford the downtime while you’re stepping in and out of code. Uptime is kind of the whole point.

With Lightrun, you can get the same level of access you get with a local debugger or profiler - no downtime required. You can add logs, metrics, and snapshots (think breakpoints, but without stopping the running service), in a safe and read-only manner - without redeploying, restarting, or even stopping the running service. Performance and security are maintained throughout the process.

Learn how to debug a live REST API (built with Spring, of course), using Lightrun, in this 5-minute tutorial:

>> Debugging REST Requests in Spring-Based applications using the Lightrun Platform

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:

public class DemoApplication {
    public static void main(String[] args) {, 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:

    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:

      uk: 6
      fr: 34
      us: 2
      uk: 8
      fr: 36
      us: 4
      uk: 10
      fr: 38
      us: 6
      uk: 12
      fr: 40
      us: 8
      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

Spring bottom

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

Java bottom

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

Generic footer banner
Comments are closed on this article!