How to parse part of a YAML file in SnakeYaml

后端 未结 2 1877

I am new to YAML and have parse a YAML config file that looks like:

applications:
  authentication:
    service-version: 2.0
    service-url: https://myapp.corp/         


        
2条回答
  •  迷失自我
    2021-02-20 09:38

    So as you have the same properties in the Auth and Service configuration I simplified that in one class to show you here.

    The YamlConfig class look like this:

    public class YamlConfig {
    
      private Map applications;
    
      //... getter and setters here
    }
    

    And the the Parser logic is something like that :

    Yaml yaml = new Yaml(new Constructor(YamlConfig.class));
    InputStream input = new FileInputStream(new File("/tmp/apps.yml"));
    YamlConfig data = yaml.loadAs( input, YamlConfig.class);
    

    I shared the full code in this gist: https://gist.github.com/marceldiass/f1d0e25671d7f47b24271f15c1066ea3

提交回复
热议问题