How to Define a Map in YAML for a POJO? – 如何在YAML中为一个POJO定义一个地图?

最后修改: 2020年 8月 18日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

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.

在本教程中,我们将学习如何使用YAML文件中定义的属性来为POJO类中的Map配置值。

2. POJO and YAML

2.POJO和YAML

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

POJO类是Plain Old Java Objects。YAML 是一种人类可读的结构化数据格式,使用缩进来表示嵌套。

2.1. Simple Map Example

2.1.简单的Map例子

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:

让我们想象一下,我们正在经营一家网上商店,我们正在创建一个翻译服装尺寸的服务。起初,我们只销售英国的衣服。我们想知道标签 “S”、”M”、”L “等是指什么英国尺寸。我们创建我们的POJO配置类。

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

注意@ConfigurationPropertiesprefix值。我们将在YAML文件中的同一根值下定义我们的映射,我们可以在下一节看到。

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

我们还需要记住在我们的Application.class上用以下注解来启用配置属性。

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

2.2. YAML Configuration

2.2.YAML 配置

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

现在我们在YAML配置中添加t-shirt-size

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

我们可以在我们的application.yml文件中使用以下结构。

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.

注意缩进和空格。YAML使用缩进来表示嵌套。推荐的语法是每个嵌套层有两个空格。

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.

注意到我们是如何使用simple-mapping与破折号的,但我们的类中的属性名称叫做simpleMapping。 带有破折号的YAML属性在代码中会自动翻译成骆驼大写字母。

2.3. More Complex Map Example

2.3.更复杂的Map例子

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.

在我们成功的英国商店之后,我们现在需要考虑将尺寸翻译成其他国家的尺寸。例如,我们现在想知道在法国和美国的标签 “S “是什么尺寸。我们需要在我们的配置中增加另一层数据。

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

我们可以用一个更复杂的映射来改变我们的application.yml

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:

我们的POJO中的相应字段将是一个地图的地图。

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

3. Conclusion

3.总结

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

在这篇文章中,我们看到了如何在YAML配置文件中为一个简单的POJO定义简单和更复杂的嵌套地图。

The code for this article is available over on GitHub

本文的代码可在GitHub上找到over