SnakeYaml 错误 无法创建属性错误

huangapple go评论90阅读模式
英文:

SnakeYaml error cannot create property error

问题

以下是您提供的代码和错误信息的翻译部分:

我正在尝试使用SnakeYaml读取和写入YAML文件
以下是代码

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);

        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;
    }


}

YAML文件如下:

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

以下是错误信息的翻译:

> 在线程"main"中出现异常,无法为JavaBean=oe.kubeapi.abc.Values@1372ed45创建属性=image,位于'reader'中,第1行,第1列:
>     id: "20"
>     ^ 在'reader'中找不到类oe.kubeapi.abc.Image的单参数构造函数,位于第7行,第3列:
>       repository: "test.abc.com/test ...
>       ^
> 不确定缺少哪个属性,请提供建议

请注意,您的错误消息指出在YAML文件中的image部分缺少属性,因此需要确保Image类具有与YAML文件中的字段相对应的属性和构造函数。

英文:

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(&quot;C:\\yaml\\student.yaml&quot;));
Representer representer = new Representer();
representer.getPropertyUtils().setSkipMissingProperties(true);
Yaml yaml = new Yaml(new Constructor(Values.class),representer);
//	Map&lt;String, Object&gt; data = yaml.load(inputStream);
Values data = yaml.load(inputStream);
System.out.println(data);
}
}
public class Values {
private List&lt;Image&gt; 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&lt;Image&gt; 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&lt;Image&gt; getImage() {
return image;
}
public void setImage(List&lt;Image&gt; 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: &quot;20&quot;
name: &quot;Bruce&quot;
year: &quot;2020&quot;
address: &quot;Gotham City&quot;
department: &quot;Computer Science&quot;
image:
repository: &quot;test.abc.com/test&quot;
pullPolicy: &quot;IfNotPresent&quot;
tag: &quot;xyz&quot;

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

答案1

得分: 1

问题在于您如何构造了yaml文件。Values 类中 image 属性的类型是 List&lt;Image&gt;

public class Values {
    private List&lt;Image&gt; image;
    // setter &amp; getters &amp; constructors
}

如果您的 yaml 文件如下所示:

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

那么您在说 image 属性是一个具有三个属性(repositorypullPolicytag)的对象。您还可以将其视为具有三个键值对的字典,而不是列表。

有不同的方法来处理它,所有这些方法都在yaml规范中有记录。

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

如果您将以下行添加到您的 main 方法中:

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

您将看到 Image 类的 tag 属性在所有情况下都被打印出来。

英文:

The problem is how you structured the yaml file. The type of image attribute in class Value is a List&lt;Image&gt;.

public class Values {
    private List&lt;Image&gt; image;
    // setter &amp; getters &amp; constructors
}

with your yaml file like

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

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: &quot;test.abc.com/test1&quot;, pullPolicy: &quot;IfNotPresent1&quot;, tag: &quot;xyz1&quot; },
    { repository: &quot;test.abc.com/test2&quot;, pullPolicy: &quot;IfNotPresent2&quot;, tag: &quot;xyz2&quot; }
  ]
image:
  - { repository: &quot;test.abc.com/test1&quot;, pullPolicy: &quot;IfNotPresent1&quot;, tag: &quot;xyz1&quot; }
  - { repository: &quot;test.abc.com/test2&quot;, pullPolicy: &quot;IfNotPresent2&quot;, tag: &quot;xyz2&quot; }
image:
  - repository: &quot;test.abc.com/test1&quot;
    pullPolicy: &quot;IfNotPresent1&quot;
    tag: &quot;xyz1&quot;
  - repository: &quot;test.abc.com/test2&quot;
    pullPolicy: &quot;IfNotPresent2&quot;
    tag: &quot;xyz2&quot;

if you add the following lines to your main method

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

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

huangapple
  • 本文由 发表于 2023年5月28日 22:27:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76351978.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定