我使用Jackson的@JsonIdentityInfo
注释来生成漂亮的对象图。
我有以下对象(也是一个hibernate实体):
@Entity
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class,
property="id")
public class MemberFieldDef implements Def
{
@Id @GeneratedValue(generator="myGen")
private String id;
....
}
这个类在序列化期间工作正常,当时是对象图的一部分,但是当我想通过杰克逊提交一个新的瞬态实体时,该实体还没有身份证明
{
"id":null,
...
}
反序列化失败了:
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)
堆栈跟踪显示Jackson没有使用常规的StringDeserializer - 它为文字null返回null - 但使用的ObjectIdValueProperty
似乎不允许空值。
是否有瞬态对象的解决方案?
答案 0 :(得分:1)
答案 1 :(得分:0)
我知道这个是问题,但我能够通过将返回目标对象的getter编码为以下内容来解决此问题:
public MemberFieldDef getMemberFieldDef() {
return (this.memberFieldDef == null || this.memberFieldDef.getId() == null)?null:this.memberFieldDef;
}
另外,如果您使用ID作为hashCode的一部分,请确保您的equals / hashCode占空值。