javayamlsnakeyaml

SnakeYaml error cannot create property error


I'm trying to read and write the yaml file using SnakeYaml. here is the code :

public class SnakeYaml {

    public static void main(String[] args) throws FileNotFoundException {
        // TODO Auto-generated method stub
        
        InputStream inputStream = new FileInputStream(new File("C:\\yaml\\student.yaml"));
    
        Representer representer = new Representer();
         representer.getPropertyUtils().setSkipMissingProperties(true);

        Yaml yaml = new Yaml(new Constructor(Values.class),representer);
    //  Map<String, Object> data = yaml.load(inputStream);
        
        Values data = yaml.load(inputStream);
        System.out.println(data);

    }
}

public class Values {
    
     private List<Image> image;
     private String id;
     private String name;
     private String year;
     private String address;
     public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getYear() {
        return year;
    }

    public void setYear(String year) {
        this.year = year;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getDepartment() {
        return department;
    }

    public void setDepartment(String department) {
        this.department = department;
    }

    private String department;
     
     
     public Values()
     {
         
     }
     
     public Values (List<Image> image, String id, String name, String year, String address, String department )
     {
         super ();
         this.image = image;
         this.id = id;
         this.name = name;
         this.year = year;
         this.address = address;
         this.department = department;
         
         
     }

    public List<Image> getImage() {
        return image;
    }

    public void setImage(List<Image> image) {
        this.image = image;
    }
}


public class Image {
    
    private String repository;
    private String pullPolicy;
    private String tag;
    
    public Image()
    {
        
    }
    
    public Image (String repository, String pullPolicy, String tags)
    {
        super();
        this.repository = repository;
        this.pullPolicy = pullPolicy;
        this.tag = tags;
        
    }
    
    public String getRepository() {
        return repository;
    }
    public void setRepository(String repository) {
        this.repository = repository;
    }
    public String getPullPolicy() {
        return pullPolicy;
    }
    public void setPullPolicy(String pullPolicy) {
        this.pullPolicy = pullPolicy;
    }
    public String getTag() {
        return tag;
    }
    public void setTag(String tag) {
        this.tag = tag;
    }
    

}

The yaml file is as below :

id: "20"
name: "Bruce"
year: "2020"
address: "Gotham City"
department: "Computer Science"
image:
  repository: "test.abc.com/test"
  pullPolicy: "IfNotPresent"
  tag: "xyz"

getting below error

Exception in thread "main" Cannot create property=image for JavaBean=oe.kubeapi.abc.Values@1372ed45 in 'reader', line 1, column 1: id: "20" ^ No single argument constructor found for class oe.kubeapi.abc.Image in 'reader', line 7, column 3: repository: "test.abc.com/test ... ^

not sure which property is missing . please suggest


Solution

  • The problem is how you structured the yaml file. The type of image attribute in class Value is a List<Image>.

    public class Values {
        private List<Image> image;
        // setter & getters & constructors
    }
    

    with your yaml file like

    id: "20"
    name: "Bruce"
    year: "2020"
    address: "Gotham City"
    department: "Computer Science"
    image:
      repository: "test.abc.com/test"
      pullPolicy: "IfNotPresent"
      tag: "xyz"
    

    you are saying that image property is an object with three attributes (repository, pullPolicy, tag). You could also consider it as a dictionary with three key-value pairs, but not a list.

    There are different ways of doing it. All of them are documented in the spec of yaml

      [
        { repository: "test.abc.com/test1", pullPolicy: "IfNotPresent1", tag: "xyz1" },
        { repository: "test.abc.com/test2", pullPolicy: "IfNotPresent2", tag: "xyz2" }
      ]
    
    image:
      - { repository: "test.abc.com/test1", pullPolicy: "IfNotPresent1", tag: "xyz1" }
      - { repository: "test.abc.com/test2", pullPolicy: "IfNotPresent2", tag: "xyz2" }
    
    image:
      - repository: "test.abc.com/test1"
        pullPolicy: "IfNotPresent1"
        tag: "xyz1"
      - repository: "test.abc.com/test2"
        pullPolicy: "IfNotPresent2"
        tag: "xyz2"
    

    if you add the following lines to your main method

    Values data = yaml.load(inputStream);
    data.getImage().forEach(x -> System.out.println(x.getTag()));
    

    you will see that the attribute tag from Image class is being printed in all the cases