Jackson Java 对象映射失败报告不存在 'known properties'

Jackson Java Object mapping fails reporting non-existent 'known properties'

我有一个 JSON 对象被映射到一个 Java 对象。 JSON 包含 Java 对象处理的嵌套数据。我的案例很大,所以我将使用简化版本作为我正在尝试的示例。

我的 JSON 数据如下所示:

{"name":"wmi", "data": [{"ip":"192.168.1.50", "L2CacheSize":"1024", "L2CacheSpeed":"0"}, {"ip":"192.168.1.51", "L2CacheSize":"1024", "L2CacheSpeed":"0"}] }

其中数据字段包含更多 JSON 对象的列表。这是通过 Jackson 映射到我的 Java 对象,看起来像这样:

public class WmiData {
    private String name;
    private List<Computer> data;

    //Getters and Setters...

    //Constructor
    public WmiData(String name, List<Computer> computers) {
        this.name = name;
        this.data = computers;
    }

    static class Computer {
        private String ip;
        private Integer L2CacheSize;
        private Integer L2CacheSpeed;

        public Computer(String ip, Integer L2CacheSize, Integer L2CacheSpeed) {
            this.ip = ip;
            this.L2CacheSize = L2CacheSize;
            this.L2CacheSpeed = L2CacheSpeed;
        }

        //Getters and Setters...

    }
}

我正在做一个像这样的简单映射:

ObjectMapper mapper = new ObjectMapper();
WmiData data = mapper.readValue(jsonString, WmiData.class);

但是解析失败报错:

SEVERE: Failure to convert json to object:
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "L2CacheSize" (class company.Data.Wmi$Computer), not marked as ignorable (3 known properties: , "ip", "l2CacheSize", "l2CacheSpeed")
 at [Source: {"data":[{"L2CacheSize":"1024","ip":"192.168.1.50","L2CacheSpeed":"0"}, {"L2CacheSize":"1024","ip":"192.168.1.51","L2CacheSpeed":"0"}],"name":"wmi"}; line: 1, column: 26] (through reference chain: company.Data.Wmi["data"]->company.Data.Computer["L2CacheSize"])
        at com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:79)
        at com.fasterxml.jackson.databind.DeserializationContext.reportUnknownProperty(DeserializationContext.java:568)
        at com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:649)
        at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:830)
        at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:310)
        at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:112)
        at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:226)
        at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:203)
        at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:23)
        at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:338)
        at com.fasterxml.jackson.databind.deser.impl.FieldProperty.deserializeAndSet(FieldProperty.java:87)
        at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:290)
        at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:112)
        at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2563)
        at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:1805)
        at uk.co.humboldt.Sums.Service.MessageHandler.getPoll(MessageHandler.java:93)
        at uk.co.humboldt.Sums.Service.MessageHandler.pollWmi(MessageHandler.java:147)
        at uk.co.humboldt.Sums.Service.MessageHandler.pollServices(MessageHandler.java:105)
        at uk.co.humboldt.Sums.Service.MessageHandler.lambda$new[=14=](MessageHandler.java:72)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access1(ScheduledThreadPoolExecutor.java:180)
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
        at java.lang.Thread.run(Thread.java:745)

上面的错误说明 Jackson 只知道三个已知属性 ipl2CacheSizel2CacheSpeed,其中两个不存在于我的对象定义中。我的字段是 L2CacheSizeL2CacheSpeed,请注意两者的大写字母 'L'。它似乎抱怨它不兼容的数据源清楚地表明传入数据的格式与我的 Java 对象所描述的格式完全相同,但仍然失败。

可能是什么问题?

感谢任何帮助。

您看到的问题是由于 Jackson 使用 Java Bean 命名约定来计算 Java class 中的 Json 属性.

因此,要解决此问题,您可以将您的 属性 名称更改为 Jackson 所说的 "know" 或使用 @JsonProperty("L2CacheSize") 作为示例对其进行注释。

static class Computer {
    private String ip;

    @JsonProperty("L2CacheSize")
    private Integer L2CacheSize;

    @JsonProperty("L2CacheSpeed")
    private Integer L2CacheSpeed;

    public Computer(String ip, Integer L2CacheSize, Integer L2CacheSpeed) {
        this.ip = ip;
        this.L2CacheSize = L2CacheSize;
        this.L2CacheSpeed = L2CacheSpeed;
    }

    //Getters and Setters...

}

只需将 DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES 设置为 false。

ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
WmiData data = mapper.readValue(jsonString, WmiData.class);