你能帮我看看这个YAML文件吗?

时间:2018-06-12 11:31:23

标签: java yaml snakeyaml

我是YAML的新手。我试图遵循教程(https://www.java-success.com/yaml-java-using-snakeyaml-library-tutorial/),但它不完整。到目前为止,我的YAML文件看起来像这样:

tests:
- class: tests.layers.DeleteCustomLayerTest
  param: layer
  value: Test Layer
- class: tests.routes.ViewRoutesTest
  param: project
  value: SNS Project Test

我已将snakeyaml添加到我的pom.xml文件中:

<dependencies>
    <dependency>
        <groupId>org.yaml</groupId>
        <artifactId>snakeyaml</artifactId>
        <version>1.21</version>
    </dependency>

我已将snakeyaml-1.21.jar添加到libs文件夹中。

我为该文件创建了一个名为YamlTestDataFile的定义:

package otherObjects;
import java.util.Map;

public class YamlTestDataFile {

    private Map<String, String> tests;

    public Map<String, String> getTests() {
        return tests;
    }

    public void setTests(Map<String, String> tests) {
        this.tests = tests;
    }

     public Map<String, String> getTests(Map<String, String> tests) {
        this.tests = tests;
        return tests;
    }
}

最后,一个实用程序来读取文件:

package utils;

import java.io.InputStream;
import org.yaml.snakeyaml.Yaml;
import otherObjects.YamlTestDataFile;

public class ReadYamlTestData {

    public static void main(String[] args) {
        YamlTestDataFile testData = null;

        Yaml yaml = new Yaml();

        try(InputStream in = ClassLoader.getSystemResourceAsStream("testData.yml")) {
        testData = yaml.loadAs(in, YamlTestDataFile.class);
    } catch(Exception ex) {
        ex.printStackTrace();
    }

    String test =  testData.getTests().get("class");
    System.out.println("test=" + test);
  }
}

当我运行它时,我收到以下错误:

Cannot create property=tests for JavaBean=otherObjects.YamlTestDataFile@5fdef03a
 in 'reader', line 1, column 1:
tests:
^
No suitable constructor with 2 arguments found for interface java.util.Map
 in 'reader', line 2, column 1:
- class: tests.layers.DeleteCust ... 

恐怕我不知道发生了什么,也找不到任何更好解释的好教程。

继flyx的回复之后,我已经取得了一些进展。

首先,我更改了YAML文件,因此它没有使用单词class:

tests:
- className: tests.layers.DeleteCustomLayerTest
  param: layer
  value: Test Layer
- className: tests.routes.ViewRoutesTest
  param: project
  value: SNS Project Test

我的YAML文件定义如下所示:

package otherObjects;
import java.util.List;

public class YamlTestDataFile {

/* public, no-argument constructor */
public YamlTestDataFile() {

}

public static final class Test {
    private String className;
    public String param;
    public String value;

    public String getClassName() { return className; }
    public void setClassName(final String value) { className = value; }

    public String getParam() { return param; }
    public void setParam(final String value) { param = value; }

    public String getValue() { return value; }
    public void setValue(final String string) { param = string; }
}

private List<Test> tests;

public List<Test> getTests() {
    return tests;
}

public void setTests(List<Test> tests) {
    this.tests = tests;
}

}

和我的读者:

package utils;

import java.io.InputStream;
import java.util.List;

import org.yaml.snakeyaml.TypeDescription;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor;

import otherObjects.YamlTestDataFile;
import otherObjects.YamlTestDataFile.Test;

public class ReadYamlTestData {

    public static void main(String[] args) {

        Constructor constructor = new Constructor(YamlTestDataFile.class);
        TypeDescription dataDesc = new TypeDescription(YamlTestDataFile.class);
        dataDesc.putListPropertyType("tests", YamlTestDataFile.Test.class);
        constructor.addTypeDescription(dataDesc);
        Yaml yaml = new Yaml(constructor);
        InputStream in = ClassLoader.getSystemResourceAsStream("testData.yml");

        YamlTestDataFile testData = null;
        try {
            testData = yaml.loadAs(in, YamlTestDataFile.class);
        }catch(Exception e) {
            System.err.println("Exception:" + e.getMessage());
            e.printStackTrace();
        }

        List<Test> tests=testData.getTests();

        for (int x=0; x<tests.size(); x++)
        System.out.println(tests.get(x).getClassName() + ", " + tests.get(x).getParam() + ", " + tests.get(x).getValue());

    }
}

输出几乎就是我要找的东西:

tests.layers.DeleteCustomLayerTest, layer, Test Layer
tests.routes.ViewRoutesTest, project, SNS Project Test

1 个答案:

答案 0 :(得分:2)

代码中定义的类与YAML数据的结构不匹配。

在您的YAML文件中,您的根是包含一个密钥tests的映射。此键的值是一系列映射。您将此构造映射到具有tests字段Map<String, String>的类。所以你试图从YAML序列构造一个Java Map。 SnakeYaml理所当然地抱怨它不能那样做。

你的YAML结构看起来像你想做这样的事情:

public class YamlTestDataFile {
    public static final class Test {
        private String className;
        public String param;
        public String value;

        public String getClass() { return className; }
        public void setClass(final String value) { className = value; }
    }

    private List<Test> tests;

    public List<Test> getTests() {
        return tests;
    }

    public void setTests(List<Test> tests) {
        this.tests = tests;
    }
}

请注意class是Java中的关键字,因此我们需要getter和setter来加载该值。

类型擦除可防止SnakeYaml查看tests列表的通用参数。因此,我们需要告诉它这是Test的列表。让我们适当地修改加载代码:

Constructor constructor = new Constructor(YamlTestDataFile.class);
TypeDescription dataDesc = new TypeDescription(YamlTestDataFile.class);
dataDesc.putListPropertyType("tests", YamlTestDataFile.Test.class);
constructor.addTypeDescription(dataDesc);
Yaml yaml = new Yaml(constructor);
InputStream in = ClassLoader.getSystemResourceAsStream("testData.yml");
testData = yaml.loadAs(in, YamlTestDataFile.class);

提示:代码未经测试。