I am using Jackson's @JsonIdentityInfo
annotation to generate nice object graphs.
I have the following object (which is a hibernate entity also):
@Entity
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class,
property="id")
public class MemberFieldDef implements Def
{
@Id @GeneratedValue(generator="myGen")
private String id;
....
}
This class works fine during serialization, when part of an object graph, but when I want to submit a new, transient entity through Jackson, which doesn't have an ID yet and has
{
"id":null,
...
}
The deserialization fails with:
com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.String out of VALUE_NULL token
at [Source: java.io.PushbackInputStream@5950054d; line: 1, column: 2] (through reference chain: net._95point2.sarbase.cohort.entity.MemberFieldDef["id"])
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:148)
at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:762)
at com.fasterxml.jackson.databind.deser.std.StringDeserializer.deserialize(StringDeserializer.java:59)
at com.fasterxml.jackson.databind.deser.std.StringDeserializer.deserialize(StringDeserializer.java:12)
at com.fasterxml.jackson.databind.deser.impl.ObjectIdValueProperty.deserializeSetAndReturn(ObjectIdValueProperty.java:90)
at com.fasterxml.jackson.databind.deser.impl.ObjectIdValueProperty.deserializeAndSet(ObjectIdValueProperty.java:82)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:306)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeWithObjectId(BeanDeserializerBase.java:1036)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:122)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3066)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2221)
at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:205)
The stacktrace shows that Jackson is not using the regular StringDeserializer - which returns null for a literal null - but is using the ObjectIdValueProperty
which doesn't seem to permit nulls.
Is there a solution for transient objects?
We fixed it using the solution provided here, using JsonInclude:
@JsonInclude(Include.NON_NULL)
public class Shop {
//...
}
Hope it works for you as well!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With