how to convert yml file to java pojo

时光怂恿深爱的人放手 提交于 2020-01-06 07:03:00

问题


This is the yaml file. I am trying this to load into pojo class. but it shows me cannot create property for listMap. Please help me in digging out this problem. In this yaml file i have a listMap which contains multiple key value pairs. Also i need advice whether this approach is better or i should load it into Map.

android: dfeiei
driver: dfkejifein
list:
  - aabra
  - ka
  - dabra
listMap:
  key: value
  name: paras
  sirname: porwal

code to load this yaml file into pojo.

YamlLoader yamlFile=yaml.loadAs(in, YamlLoader.class);

Here is the POJO class .

 import java.util.List;
    import java.util.Map;

    public class YamlLoader {
    private String android;
    private String driver;
    private List<String>list;
    private Map<String,String>map;
    private List listMap;
    public String getAndroid() {
        return android;
    }
    public void setAndroid(String android) {
        this.android = android;
    }
    public String getDriver() {
        return driver;
    }
    public void setDriver(String driver) {
        this.driver = driver;
    }
    public List<String> getList() {
        return list;
    }
    public void setList(List<String> list) {
        this.list = list;
    }

    @Override
    public String toString() {
        return "YamlLoader [android=" + android + ", driver=" + driver + ", list=" + list + ", map=" + map + ", listMap="
                + listMap + "]";
    }
    public Map<String,String> getMap() {
        return map;
    }
    public void setMap(Map<String,String> map) {
        this.map = map;
    }
    public List getListMap() {
        return listMap;
    }
    public void setListMap(List listMap) {
        this.listMap = listMap;
    }

回答1:


listMap is a List, which is why YAML cannot fill it from a mapping. If you want a list, change your YAML like this:

listMap:
  - key: value
  - name: paras
  - sirname: porwal

This will get you a list of single-entry mappings. If you want to keep the mapping on the YAML side, load it into a Map.

If you want to have more control over how YAML entities map to Java types, use the SnakeYaml API instead of Jackson. I never get why people use Jackson for YAML processing when it offers fewer customizations than SnakeYaml itself and at the same time is not at any higher level.



来源:https://stackoverflow.com/questions/45706583/how-to-convert-yml-file-to-java-pojo

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!