简体   繁体   English

无法识别的字段Jackson YAML Reader

[英]Unrecognized field Jackson YAML Reader

Main.java Main.java

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

Model.java 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 yamlfile.yaml

details:
   topic: "test"
   id: "123"
   servers: [
            "test2"
            ]

I run main.java and get an exception: 我运行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"])

I have no idea what I am doing wrong here and I cant seem to find out the problem. 我不知道我在做什么错,我似乎无法找出问题所在。 Why is this unrecognized? 为什么这无法识别?

By default Jackson can access public fields for serialization and deserialization. 默认情况下,Jackson可以访问公共字段进行序列化和反序列化。 If there's no public fields available then public getters/setters are used. 如果没有可用的公共字段,则使用公共获取者/设置者。

So: add getters/setters or add @JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY) at the class level. 因此:在类级别添加getter / setter或添加@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY) For both classes, sure. 对于两个类,都可以。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM