I have a JSON object that is being mapped to a Java Object. The JSON contains nested data which the Java Object handles. My case is large so I will use a cut down version as an example of what I am attempting.
My JSON data looks like this:
{"name":"wmi", "data": [{"ip":"192.168.1.50", "L2CacheSize":"1024", "L2CacheSpeed":"0"}, {"ip":"192.168.1.51", "L2CacheSize":"1024", "L2CacheSpeed":"0"}] }
where the data field holds a list of further JSON objects. This is mapped via Jackson to my Java Object that looks like this:
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...
}
}
and I am doing a simple mapping like this:
ObjectMapper mapper = new ObjectMapper();
WmiData data = mapper.readValue(jsonString, WmiData.class);
However, the parsing fails giving an error:
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)
The error above explains that Jackson is only aware of three known properties ip
, l2CacheSize
and l2CacheSpeed
, two of which do not exist in my object definition. My fields are L2CacheSize
and L2CacheSpeed
, note the capital 'L' in both. The data source it seems to complain it is incompatible with clearly shows that the incoming data is in the exact same format as described by my Java Objects but yet fails.
What could be the problem?
Any help is appreciated.
The problem you are seeing is due to the fact that Jackson uses Java Bean naming conventions, to figure it out the Json properties in a Java class.
So, to solve it you can change your property names to the ones Jackson says he "know" or annotate it with @JsonProperty("L2CacheSize")
as example.
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...
}