Convert .Yaml to Java Classes Automatically On Application

103 Views Asked by At

I am using Maven with Java and have a .yaml file

data:
  users:
    id: "int"
    name: "string"
    email: "string"
    phone:
      - "string"
    address:
      line1: "string"
      line2: "string"
      zip: "int"
database:
  url: "any-dbs-url"
  username: "username"
  password: "password"

I want to be converted into a Java class when the application starts, this should be an automatic process, each time the application starts the classes should be created according to the changes in the .yaml file.

How can I achieve it? any suggestion, code or source would be very helpful.

I tried using snakeyaml dependency and created a class manually.

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

Yaml yaml = new Yaml();
InputStream inputStream = new FileInputStream("file location\\yml-file-name.yml");

ManualClass manualClass = yaml.loadAs(inputStream,ManualClass.class);
System.out.println("manualClass = " + manualClass);

But manually creating classes for a lot of yaml files won't be feasible.

0

There are 0 best solutions below