gpt4 book ai didi

java - SnakeYaml 不使用我的二传手

转载 作者:行者123 更新时间:2023-12-05 07:36:47 26 4
gpt4 key购买 nike

我正在尝试使用 Java 中的 SnakeYaml 解析 YAML 文件,并且我正在努力让它通过 setter 构建我的模型对象。我从 SnakeYaml 文档中了解到,当您使用遵循 JavaBean 规则的对象时,SnakeYaml 使用反射来查找 setter 并使用它们来实例化成员。

我将代码缩减为以下极简版本,但它仍然无法正常工作:

这是我的模型对象:

public class Report {

private String MainSourceFile;

public Report() {
super();
}

public String getMainSourceFile() {
return this.MainSourceFile;
}

public void setMainSourceFile(String mainSourceFile) {
this.MainSourceFile = mainSourceFile;
}
}

这是解析 YAML 文件的代码:

Constructor constructor = new Constructor(Report.class);

Yaml yaml = new Yaml(constructor);
Report report = null;

try (InputStream str = new FileInputStream(file);) {
report = (Report) yaml.load(str);
} catch (FileNotFoundException e) {
throw new ParsingErrorException( String.format("Error while reading export file %s : file not found", file.getAbsolutePath()), e);
} catch (YAMLException e) {
throw new ParsingErrorException(String.format("Error while parsing export file %s", file.getAbsolutePath()), e);
} catch (IOException e1) {
Activator.logWarning(String.format("Error while closing file %s", file.getAbsolutePath()));
}

这是 YAML 文件:

---
MainSourceFile: /home/user/workspace/project/ex.c

抛出如下错误

Unable to find property 'MainSourceFile' on class: fr.xxx.xxx.model.Report
in 'reader', line 2, column 18:
MainSourceFile: /home/user/workspace/project/ex.c
^

at org.yaml.snakeyaml.constructor.Constructor$ConstructMapping.constructJavaBean2ndStep(Constructor.java:268)
at org.yaml.snakeyaml.constructor.Constructor$ConstructMapping.construct(Constructor.java:149)
at org.yaml.snakeyaml.constructor.Constructor$ConstructYamlObject.construct(Constructor.java:308)
at org.yaml.snakeyaml.constructor.BaseConstructor.constructObjectNoCheck(BaseConstructor.java:207)
at org.yaml.snakeyaml.constructor.BaseConstructor.constructObject(BaseConstructor.java:196)
at org.yaml.snakeyaml.constructor.BaseConstructor.constructDocument(BaseConstructor.java:161)
at org.yaml.snakeyaml.constructor.BaseConstructor.getSingleData(BaseConstructor.java:147)
at org.yaml.snakeyaml.Yaml.loadFromReader(Yaml.java:524)
at org.yaml.snakeyaml.Yaml.load(Yaml.java:452)
at fr.xxx.xxx.ErrorParser.parseFixesExportFile(ErrorParser.java:116)
... 24 more
Caused by: org.yaml.snakeyaml.error.YAMLException: Unable to find property 'MainSourceFile' on class: fr.xxx.xxx.model.Report
at org.yaml.snakeyaml.introspector.PropertyUtils.getProperty(PropertyUtils.java:189)
at org.yaml.snakeyaml.introspector.PropertyUtils.getProperty(PropertyUtils.java:178)
at org.yaml.snakeyaml.TypeDescription.discoverProperty(TypeDescription.java:240)
at org.yaml.snakeyaml.TypeDescription.getProperty(TypeDescription.java:251)
at org.yaml.snakeyaml.constructor.Constructor$ConstructMapping.constructJavaBean2ndStep(Constructor.java:210)
... 33 more

如果我将 MainSourceFile 成员声明为 public,解析工作(这就是它以大写字母开头的原因)。但我不想让它 public 而且,我想使用 setter。我不明白它不使用 setter 的原因:我的 Report 类对我来说似乎是一个合法的 JavaBean。

还有什么我想念的吗?

最佳答案

MainSourceFile 应该是 mainSourceFile。

关于java - SnakeYaml 不使用我的二传手,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48964287/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com