问题描述
尝试使用@JsonIdentityInfo jackson注释时出错。当我尝试反序列化对象时,我得到以下异常:
I have an error when trying to work with @JsonIdentityInfo jackson annotation. When I try to deserialize the object I get the folowing exception:
我试图反序列化的JSON看起来像:
The JSON I am trying to deserialize looks like:
{
"@Site": 1,
"siteId": 1,
"name": "0",
"address": {
"@Address": 2,
"addressId": 4,
"number": "22"
},
"operators": [
{
"accountId": 1,
"email": "[email protected]",
"image": {
"@Image": 1,
"imageId": 1,
"uri": "http://icons.iconarchive.com/icons/deleket/purple-monsters/128/Alien-awake-icon.png"
}
},
{
"accountId": 2,
"email": "[email protected]",
"image": {
"@Image": 2,
"imageId": 2,
"uri": "http://icons.iconarchive.com/icons/deleket/purple-monsters/128/Alien-awake-icon.png"
}
}
]
}
我的域对象注释为
@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class, property = "@Image")
问题出现在@Id注释上,因为如果我删除注释,问题就会消失(就像我对帐户所做的那样),但根据我的理解,新功能对循环依赖有用,这对我在其他场景中很有用。两个图像之间不应该存在冲突,因为它们是不同的对象。
The problem arises on @Id annotation since if I remove the annotation the problem disappears (as i did for account) but on my understanding the new feature is usefull for cyclic dependencies wich is usefull for me in other scenarios. There shouldn't be a conflict between the 2 images since they are differente objects.
是否有人知道如何解决或者问题是什么?
Does anybody have an idea on how to solve or what is the problem?
推荐答案
在注释ID时,应使用范围
参数。然后解串器将确保id在范围内是唯一的。
You should use scope
parameter when annotating the ids. Then the de-serializer would make sure the id is unique within the scope.
来自注释类型 JsonIdentityInfo
:
例如。 @JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class,property =@ id,scope = Account.class)
这篇关于JsonMappingException:已经有id的POJO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!