Java 如何在 YAML 文件中为简单的 POJO 定义映射?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34328749/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 15:32:31  来源:igfitidea点击:

How to define a map in a YAML file for simple POJO?

javayamlpojosnakeyaml

提问by frugalcoder

I am using snakeYaml to parse certain configuration/property values to a Configuration object.

我正在使用 snakeYaml 将某些配置/属性值解析为 Configuration 对象。

My yaml file looks like this -

我的 yaml 文件看起来像这样 -

#Thread
batchLimit: 1000
threadCountLimit: 2

#Some More Config
key: value

#MAP
keyMapping: <What goes here?>

My Configuration class looks like this -

我的配置类看起来像这样 -

public class Configuration{
  int batchlimit;
  int threadCountLimit;
  ...
  Map<String,String> keyMapping;
}

How do I define the keyMappingin the YAML file so it reads directly through SnakeYAML?

如何keyMapping在 YAML 文件中定义以便它直接通过 SnakeYAML 读取?

采纳答案by AlexR

Here is how it can look like:

这是它的样子:

#MAP
keyMapping: 
    key1: value1
    key2: value2

Generally YAML format has natural support of key-value pairs. Take a look on the following tutorial (just for example): https://github.com/Animosity/CraftIRC/wiki/Complete-idiot's-introduction-to-yaml

通常 YAML 格式自然支持键值对。看看下面的教程(只是举例):https: //github.com/Animosity/CraftIRC/wiki/Complete-idiot's-introduction-to-yaml

Or just google "yaml map" for more details.

或者只是谷歌“yaml map”了解更多详情。

回答by Māris Rubenis

Yaml file "AppParams.yml" with key-value pairs:

带有键值对的 Yaml 文件“AppParams.yml”:

someConfig:
    key1: value1
    key2: value2

POJO:

POJO:

public class ApplicationParameters {
    private Map<String, String> someConfig;

    public ApplicationParameters() {
    }

    public Map<String, String> getSomeConfig() {
        return someConfig;
    }

    public void setSomeConfig(Map<String, String> someConfig) {
        this.someConfig = someConfig;
    }
}

Reader:

读者:

ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
File paramFile = new File("AppParams.yml");
ApplicationParameters applicationParameters = mapper.readValue(paramFile, ApplicationParameters.class);

Map<String, String> someConfig = applicationParameters.getSomeConfig();

String key1Value = someConfig.get("key1");    //returns "value1"

The example above uses these dependencies in POM.xml:

上面的示例在 POM.xml 中使用了这些依赖项:

 <dependencies>
    <dependency>
        <groupId>com.fasterxml.Hymanson.core</groupId>
        <artifactId>Hymanson-core</artifactId>
        <version>2.9.8</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.Hymanson.core</groupId>
        <artifactId>Hymanson-databind</artifactId>
        <version>2.9.8</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.Hymanson.dataformat</groupId>
        <artifactId>Hymanson-dataformat-yaml</artifactId>
        <version>2.9.8</version>
    </dependency>
</dependencies>