gpt4 book ai didi

java - 无法识别的字段 Jackson YAML Reader

转载 作者:行者123 更新时间:2023-11-30 05:48:33 26 4
gpt4 key购买 nike

Main.java

 ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
Model k = mapper.readValue(new File(PATH), Model.class);

模型.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"
]

我运行ma​​in.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"])

我不知道我在这里做错了什么,而且我似乎无法找出问题所在。为什么这个不被认可?

最佳答案

默认情况下,Jackson 可以访问公共(public)字段以进行序列化和反序列化。如果没有可用的公共(public)字段,则使用公共(public) getter/setter。

所以:添加 getter/setter 或添加 @JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY)在类(class)层面。对于这两个类,当然。

关于java - 无法识别的字段 Jackson YAML Reader,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54410430/

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