SnakeYaml 错误 无法创建属性错误

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

SnakeYaml error cannot create property error

问题

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

  1. 我正在尝试使用SnakeYaml读取和写入YAML文件
  2. 以下是代码
  3. public class SnakeYaml {
  4. public static void main(String[] args) throws FileNotFoundException {
  5. // TODO Auto-generated method stub
  6. InputStream inputStream = new FileInputStream(new File("C:\\yaml\\student.yaml"));
  7. Representer representer = new Representer();
  8. representer.getPropertyUtils().setSkipMissingProperties(true);
  9. Yaml yaml = new Yaml(new Constructor(Values.class), representer);
  10. Values data = yaml.load(inputStream);
  11. System.out.println(data);
  12. }
  13. }
  14. public class Values {
  15. private List<Image> image;
  16. private String id;
  17. private String name;
  18. private String year;
  19. private String address;
  20. public String getId() {
  21. return id;
  22. }
  23. public void setId(String id) {
  24. this.id = id;
  25. }
  26. public String getName() {
  27. return name;
  28. }
  29. public void setName(String name) {
  30. this.name = name;
  31. }
  32. public String getYear() {
  33. return year;
  34. }
  35. public void setYear(String year) {
  36. this.year = year;
  37. }
  38. public String getAddress() {
  39. return address;
  40. }
  41. public void setAddress(String address) {
  42. this.address = address;
  43. }
  44. public String getDepartment() {
  45. return department;
  46. }
  47. public void setDepartment(String department) {
  48. this.department = department;
  49. }
  50. private String department;
  51. public Values() {
  52. }
  53. public Values(List<Image> image, String id, String name, String year, String address, String department) {
  54. super();
  55. this.image = image;
  56. this.id = id;
  57. this.name = name;
  58. this.year = year;
  59. this.address = address;
  60. this.department = department;
  61. }
  62. public List<Image> getImage() {
  63. return image;
  64. }
  65. public void setImage(List<Image> image) {
  66. this.image = image;
  67. }
  68. }
  69. public class Image {
  70. private String repository;
  71. private String pullPolicy;
  72. private String tag;
  73. public Image() {
  74. }
  75. public Image(String repository, String pullPolicy, String tags) {
  76. super();
  77. this.repository = repository;
  78. this.pullPolicy = pullPolicy;
  79. this.tag = tags;
  80. }
  81. public String getRepository() {
  82. return repository;
  83. }
  84. public void setRepository(String repository) {
  85. this.repository = repository;
  86. }
  87. public String getPullPolicy() {
  88. return pullPolicy;
  89. }
  90. public void setPullPolicy(String pullPolicy) {
  91. this.pullPolicy = pullPolicy;
  92. }
  93. public String getTag() {
  94. return tag;
  95. }
  96. public void setTag(String tag) {
  97. this.tag = tag;
  98. }
  99. }

YAML文件如下:

  1. id: "20"
  2. name: "Bruce"
  3. year: "2020"
  4. address: "Gotham City"
  5. department: "Computer Science"
  6. image:
  7. repository: "test.abc.com/test"
  8. pullPolicy: "IfNotPresent"
  9. tag: "xyz"

以下是错误信息的翻译:

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

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

英文:

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

  1. public class SnakeYaml {
  2. public static void main(String[] args) throws FileNotFoundException {
  3. // TODO Auto-generated method stub
  4. InputStream inputStream = new FileInputStream(new File(&quot;C:\\yaml\\student.yaml&quot;));
  5. Representer representer = new Representer();
  6. representer.getPropertyUtils().setSkipMissingProperties(true);
  7. Yaml yaml = new Yaml(new Constructor(Values.class),representer);
  8. // Map&lt;String, Object&gt; data = yaml.load(inputStream);
  9. Values data = yaml.load(inputStream);
  10. System.out.println(data);
  11. }
  12. }
  13. public class Values {
  14. private List&lt;Image&gt; image;
  15. private String id;
  16. private String name;
  17. private String year;
  18. private String address;
  19. public String getId() {
  20. return id;
  21. }
  22. public void setId(String id) {
  23. this.id = id;
  24. }
  25. public String getName() {
  26. return name;
  27. }
  28. public void setName(String name) {
  29. this.name = name;
  30. }
  31. public String getYear() {
  32. return year;
  33. }
  34. public void setYear(String year) {
  35. this.year = year;
  36. }
  37. public String getAddress() {
  38. return address;
  39. }
  40. public void setAddress(String address) {
  41. this.address = address;
  42. }
  43. public String getDepartment() {
  44. return department;
  45. }
  46. public void setDepartment(String department) {
  47. this.department = department;
  48. }
  49. private String department;
  50. public Values()
  51. {
  52. }
  53. public Values (List&lt;Image&gt; image, String id, String name, String year, String address, String department )
  54. {
  55. super ();
  56. this.image = image;
  57. this.id = id;
  58. this.name = name;
  59. this.year = year;
  60. this.address = address;
  61. this.department = department;
  62. }
  63. public List&lt;Image&gt; getImage() {
  64. return image;
  65. }
  66. public void setImage(List&lt;Image&gt; image) {
  67. this.image = image;
  68. }
  69. }
  70. public class Image {
  71. private String repository;
  72. private String pullPolicy;
  73. private String tag;
  74. public Image()
  75. {
  76. }
  77. public Image (String repository, String pullPolicy, String tags)
  78. {
  79. super();
  80. this.repository = repository;
  81. this.pullPolicy = pullPolicy;
  82. this.tag = tags;
  83. }
  84. public String getRepository() {
  85. return repository;
  86. }
  87. public void setRepository(String repository) {
  88. this.repository = repository;
  89. }
  90. public String getPullPolicy() {
  91. return pullPolicy;
  92. }
  93. public void setPullPolicy(String pullPolicy) {
  94. this.pullPolicy = pullPolicy;
  95. }
  96. public String getTag() {
  97. return tag;
  98. }
  99. public void setTag(String tag) {
  100. this.tag = tag;
  101. }
  102. }

The yaml file is as below :

  1. id: &quot;20&quot;
  2. name: &quot;Bruce&quot;
  3. year: &quot;2020&quot;
  4. address: &quot;Gotham City&quot;
  5. department: &quot;Computer Science&quot;
  6. image:
  7. repository: &quot;test.abc.com/test&quot;
  8. pullPolicy: &quot;IfNotPresent&quot;
  9. 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;

  1. public class Values {
  2. private List&lt;Image&gt; image;
  3. // setter &amp; getters &amp; constructors
  4. }

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

  1. id: &quot;20&quot;
  2. name: &quot;Bruce&quot;
  3. year: &quot;2020&quot;
  4. address: &quot;Gotham City&quot;
  5. department: &quot;Computer Science&quot;
  6. image:
  7. repository: &quot;test.abc.com/test&quot;
  8. pullPolicy: &quot;IfNotPresent&quot;
  9. tag: &quot;xyz&quot;

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

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

  1. [
  2. { repository: &quot;test.abc.com/test1&quot;, pullPolicy: &quot;IfNotPresent1&quot;, tag: &quot;xyz1&quot; },
  3. { repository: &quot;test.abc.com/test2&quot;, pullPolicy: &quot;IfNotPresent2&quot;, tag: &quot;xyz2&quot; }
  4. ]
  1. image:
  2. - { repository: &quot;test.abc.com/test1&quot;, pullPolicy: &quot;IfNotPresent1&quot;, tag: &quot;xyz1&quot; }
  3. - { repository: &quot;test.abc.com/test2&quot;, pullPolicy: &quot;IfNotPresent2&quot;, tag: &quot;xyz2&quot; }
  1. image:
  2. - repository: &quot;test.abc.com/test1&quot;
  3. pullPolicy: &quot;IfNotPresent1&quot;
  4. tag: &quot;xyz1&quot;
  5. - repository: &quot;test.abc.com/test2&quot;
  6. pullPolicy: &quot;IfNotPresent2&quot;
  7. tag: &quot;xyz2&quot;

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

  1. Values data = yaml.load(inputStream);
  2. 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;.

  1. public class Values {
  2. private List&lt;Image&gt; image;
  3. // setter &amp; getters &amp; constructors
  4. }

with your yaml file like

  1. id: &quot;20&quot;
  2. name: &quot;Bruce&quot;
  3. year: &quot;2020&quot;
  4. address: &quot;Gotham City&quot;
  5. department: &quot;Computer Science&quot;
  6. image:
  7. repository: &quot;test.abc.com/test&quot;
  8. pullPolicy: &quot;IfNotPresent&quot;
  9. 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

  1. [
  2. { repository: &quot;test.abc.com/test1&quot;, pullPolicy: &quot;IfNotPresent1&quot;, tag: &quot;xyz1&quot; },
  3. { repository: &quot;test.abc.com/test2&quot;, pullPolicy: &quot;IfNotPresent2&quot;, tag: &quot;xyz2&quot; }
  4. ]
  1. image:
  2. - { repository: &quot;test.abc.com/test1&quot;, pullPolicy: &quot;IfNotPresent1&quot;, tag: &quot;xyz1&quot; }
  3. - { repository: &quot;test.abc.com/test2&quot;, pullPolicy: &quot;IfNotPresent2&quot;, tag: &quot;xyz2&quot; }
  1. image:
  2. - repository: &quot;test.abc.com/test1&quot;
  3. pullPolicy: &quot;IfNotPresent1&quot;
  4. tag: &quot;xyz1&quot;
  5. - repository: &quot;test.abc.com/test2&quot;
  6. pullPolicy: &quot;IfNotPresent2&quot;
  7. tag: &quot;xyz2&quot;

if you add the following lines to your main method

  1. Values data = yaml.load(inputStream);
  2. 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:

确定