杰克逊Java对象映射无法报告不存在的已知属性'

时间:2016-11-01 17:37:14

标签: java json jackson objectmapper

我有一个映射到Java Object的JSON对象。 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$0(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.access$201(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,请注意首都&#39; L&#39;同时。它似乎抱怨它与数据源不兼容,清楚地表明传入的数据格式与我的Java对象完全相同,但却失败了。

可能是什么问题?

感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

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

因此,要解决此问题,您可以将您的属性名称更改为杰克逊所说的“知道”或以@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...

}

答案 1 :(得分:-2)

只需将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);