Main.java
ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
Model k = mapper.readValue(new File(PATH), Model.class);
Model.java
public class Model {
@JsonProperty
private Specs details;
private class Specs{
@JsonProperty
private String topic;
@JsonProperty
private String id;
@JsonProperty
private List<String> list;
}
}
yamlfile.yaml
details:
topic: "test"
id: "123"
servers: [
"test2"
]
我运行 main.java 并获得异常:
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "details" (class Model), not marked as ignorable (0 known properties: ])
at [Source: src/main/config/yamlfile.yaml; line: 2, column: 3] (through reference chain: Model["details"])
我不知道我在做什么错,而且我似乎无法找出问题所在。为什么这无法识别?
答案 0 :(得分:0)
默认情况下,Jackson可以访问公共字段以进行序列化和反序列化。如果没有可用的公共字段,则使用公共获取者/设置者。
因此:在类级别添加getter / setter或添加@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY)
。对于这两个类,请确保。