Sahil Mulla
Sahil Mulla

Reputation: 63

How to read and write data from yaml file at Run time in Java Spring?

I wanted to read and write some content from yaml file in Spring Boot application. Which parser I have to use and How? If someone knows any idea, then please elaborate with an example. thank you.

Upvotes: 0

Views: 5718

Answers (2)

Dawid Kubicki
Dawid Kubicki

Reputation: 190

You can use SnakeYaml

  1. Add dep e.g snakeyaml

  2. The Yaml class is the entry point for the API:

    Yaml yaml = new Yaml();

  3. Now we’ll parse a YAML document with the Yaml class:

    Yaml yaml = new Yaml();
    InputStream inputStream = this.getClass()
                                  .getClassLoader()
                                  .getResourceAsStream("customer.yaml");
    Map<String, Object> obj = yaml.load(inputStream);          
    System.out.println(obj);
    

Upvotes: 1

Daniele Torino
Daniele Torino

Reputation: 1812

SnakeYAML

Check the documentation for examples.

Upvotes: 3

Related Questions