为什么我不能用 SnakeYaml 和 Lombok 解析 yaml 字符串?
Why can't I parse a yaml string with SnakeYaml and Lombok?
出于某种原因,我无法通过 SnakeYaml 解析 yaml,这可能是一个基本错误,但我无法找到我错过的解决方案。
我有一颗豆子:
package main;
import lombok.Data;
import lombok.experimental.Accessors;
@Data
@Accessors(chain = true)
public class YamlConfig {
private String text;
}
和一个简单的测试
public static void loadYaml(){
Yaml yaml = new Yaml(new Constructor(YamlConfig.class));
YamlConfig config = yaml.load("text: testText");
log.info("text is {}",config.getText());
}
然而我一直得到的是
Exception in thread "main" Cannot create property=text for JavaBean=YamlConfig(text=null)
in 'string', line 1, column 1:
text: testText
^
No writable property 'text' on class: main.YamlConfig
in 'string', line 1, column 7:
text: testText
^
at org.yaml.snakeyaml.constructor.Constructor$ConstructMapping.constructJavaBean2ndStep(Constructor.java:291)
at org.yaml.snakeyaml.constructor.Constructor$ConstructMapping.construct(Constructor.java:172)
at org.yaml.snakeyaml.constructor.Constructor$ConstructYamlObject.construct(Constructor.java:332)
at org.yaml.snakeyaml.constructor.BaseConstructor.constructObjectNoCheck(BaseConstructor.java:230)
at org.yaml.snakeyaml.constructor.BaseConstructor.constructObject(BaseConstructor.java:220)
at org.yaml.snakeyaml.constructor.BaseConstructor.constructDocument(BaseConstructor.java:174)
at org.yaml.snakeyaml.constructor.BaseConstructor.getSingleData(BaseConstructor.java:158)
at org.yaml.snakeyaml.Yaml.loadFromReader(Yaml.java:491)
at org.yaml.snakeyaml.Yaml.load(Yaml.java:416)
at main.YamlLombok.loadYaml(YamlLombok.java:16)
at main.YamlLombok.main(YamlLombok.java:11)
Caused by: org.yaml.snakeyaml.error.YAMLException: No writable property 'text' on class: main.YamlConfig
at org.yaml.snakeyaml.constructor.Constructor$ConstructMapping.constructJavaBean2ndStep(Constructor.java:235)
... 10 more
并且无法查明根本原因。
关于我做错了什么有什么建议吗?
当我将 Bean 字段切换为 public 时一切正常,但我真的很想使用 getter 和 setter(最好是那些由 Lombok 生成的)。
我无法解析的原因是
@Accessors(chain = true)
这会导致 setter return this 而不是 void。
解决方案是通过
配置 snakeyaml 以使用这些设置器
yaml.setBeanAccess(BeanAccess.FIELD);
或者,您可以设置 chain = false 或将字段设置为 public。
出于某种原因,我无法通过 SnakeYaml 解析 yaml,这可能是一个基本错误,但我无法找到我错过的解决方案。
我有一颗豆子:
package main;
import lombok.Data;
import lombok.experimental.Accessors;
@Data
@Accessors(chain = true)
public class YamlConfig {
private String text;
}
和一个简单的测试
public static void loadYaml(){
Yaml yaml = new Yaml(new Constructor(YamlConfig.class));
YamlConfig config = yaml.load("text: testText");
log.info("text is {}",config.getText());
}
然而我一直得到的是
Exception in thread "main" Cannot create property=text for JavaBean=YamlConfig(text=null)
in 'string', line 1, column 1:
text: testText
^
No writable property 'text' on class: main.YamlConfig
in 'string', line 1, column 7:
text: testText
^
at org.yaml.snakeyaml.constructor.Constructor$ConstructMapping.constructJavaBean2ndStep(Constructor.java:291)
at org.yaml.snakeyaml.constructor.Constructor$ConstructMapping.construct(Constructor.java:172)
at org.yaml.snakeyaml.constructor.Constructor$ConstructYamlObject.construct(Constructor.java:332)
at org.yaml.snakeyaml.constructor.BaseConstructor.constructObjectNoCheck(BaseConstructor.java:230)
at org.yaml.snakeyaml.constructor.BaseConstructor.constructObject(BaseConstructor.java:220)
at org.yaml.snakeyaml.constructor.BaseConstructor.constructDocument(BaseConstructor.java:174)
at org.yaml.snakeyaml.constructor.BaseConstructor.getSingleData(BaseConstructor.java:158)
at org.yaml.snakeyaml.Yaml.loadFromReader(Yaml.java:491)
at org.yaml.snakeyaml.Yaml.load(Yaml.java:416)
at main.YamlLombok.loadYaml(YamlLombok.java:16)
at main.YamlLombok.main(YamlLombok.java:11)
Caused by: org.yaml.snakeyaml.error.YAMLException: No writable property 'text' on class: main.YamlConfig
at org.yaml.snakeyaml.constructor.Constructor$ConstructMapping.constructJavaBean2ndStep(Constructor.java:235)
... 10 more
并且无法查明根本原因。
关于我做错了什么有什么建议吗?
当我将 Bean 字段切换为 public 时一切正常,但我真的很想使用 getter 和 setter(最好是那些由 Lombok 生成的)。
我无法解析的原因是
@Accessors(chain = true)
这会导致 setter return this 而不是 void。
解决方案是通过
配置 snakeyaml 以使用这些设置器yaml.setBeanAccess(BeanAccess.FIELD);
或者,您可以设置 chain = false 或将字段设置为 public。