【问题标题】:Parse YAML file to Java classes将 YAML 文件解析为 Java 类
【发布时间】:2021-05-05 13:01:00
【问题描述】:

我有一个 YAML 文件,我需要做的是创建适当的类,以便我可以使用以下代码对其进行解析:

InputStream inputStream = null;
        try {
            inputStream = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        Yaml yaml = new Yaml();
        Root myYaml = yaml.loadAs(inputStream, Root.class);

这是 YAML 文件:

project:
  name: My Software Project
  description: The description of the project
suites:
  - BackEndTests:
    - Test1:
        enabled: true
        command: echo Test
    - Test2:
        enabled: true
        command: echo Test 2
        description: This test does something too
  - UITests:
    - Test3:
        enabled: true
        command: echo Test 3
        description: Hello world
    - Test4:
        enabled: false
        command: echo Test 4

我创建了以下类:

public class Root {
     private Project project;
     private List<Suite> suites;
}
// setters and getters

public class Project {
     private String name;
     private String description;
}
// setters and getters

public class Suite {
     private String name;
     private List<Test> tests;
}
// setters and getters

public class Test {
     private boolean enabled;
     private String command;
     private String description;
}
// setters and getters

我的应用程序收到此错误消息,使用 Spring Boot 构建: Click here to see full error message

我总是为每个类包含 setter、getter 甚至构造函数。有人可以建议如何构建类,以便我可以解析 YAML 文件而不会出现任何错误?另外,是否可以在类中添加额外的字段(除了 yaml 中列出的这些),因为我以后需要将它们用于其他目的?一个例子:

public class Project {
     private Integer id; // adding field that is not contained in the yaml
     private String name;
     private String description;
}

提前致谢!

【问题讨论】:

  • 我建议尝试从数据中生成 YML,并将其与您认为正确的 YML 进行比较。这可能会给你一些见解。
  • 欢迎来到 SO!请注意,他们没有解决不是错误描述。请编辑您的问题以包含对您遇到的行为的描述。另外,在描述 Java 类时请坚持 Java 语法,并且通常尝试提供代码为minimal, reproducible example,以便其他人可以重现您的问题。
  • 根据您共享的 YAML,您必须在套件类内部添加两个名为 BackEndTests 和 UITests 的对象。并且在这些类中将有 Test 的对象,其名称分别为 Test1 和 Test2,...您不能使用 List,因为这两个对象都有不同的名称
  • 在你的 YAML 中有相似的对象,但你仍然不能使用 List,因为它们都有不同的键名。您可以使用相同的类,但您必须为每个对象分别声明对象
  • @RanjanMohanty 问题是应用程序每次运行时都必须解析不同的 YAML,并使用不同的测试次数。

标签: java yaml snakeyaml


【解决方案1】:

如果你想使用 YAML 格式,那么正确的格式应该是

project:
  description: The description of the project
  name: My Software Project
suites:
  - name: BackEndTests
    tests:
      - command: echo Test
        enabled: true
        name: Test1
      - command: echo Test 2
        description: This test does something too
        enabled: true
        name: Test 2
  - name: UITests
    tests:
      - command: echo Test 3
        description: Hello world
        enabled: true
        name: Test3
      - command: echo Test 4
        enabled: false
        name: Test4

课程将是:

public class Root {
     private Project project;
     private List<Suite> suites;
}
// setters and getters

public class Project {
     private String name;
     private String description;
}
// setters and getters

public class Suite {
     private String name;
     private List<Test> tests;
}
// setters and getters

public class Test {
     private String name;
     private boolean enabled;
     private String command;
     private String description;
}
// setters and getters

【讨论】:

    猜你喜欢
    • 2019-10-17
    • 1970-01-01
    • 2021-08-23
    • 2014-11-05
    • 1970-01-01
    • 2018-04-27
    相关资源
    最近更新 更多