英文:
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("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
答案1
得分: 1
问题在于您如何构造了yaml文件。Values
类中 image
属性的类型是 List<Image>
。
public class Values {
private List<Image> image;
// setter & getters & constructors
}
如果您的 yaml 文件如下所示:
id: "20"
name: "Bruce"
year: "2020"
address: "Gotham City"
department: "Computer Science"
image:
repository: "test.abc.com/test"
pullPolicy: "IfNotPresent"
tag: "xyz"
那么您在说 image
属性是一个具有三个属性(repository、pullPolicy、tag)的对象。您还可以将其视为具有三个键值对的字典,而不是列表。
有不同的方法来处理它,所有这些方法都在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"
如果您将以下行添加到您的 main
方法中:
Values data = yaml.load(inputStream);
data.getImage().forEach(x -> 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<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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论