杰克逊的JsonIdentityInfo生成普通数字

时间:2014-05-05 19:43:00

标签: json spring jackson jsonidentityinfo

使用Spring,3.2.7,Hibernate 4.2.12,Jackson 2.3.3 并使用JsonIdentityInfo在jackson中尝试引用。

我有一个实体的taxanomy,父母和孩子,都提到了Taxanomy self。

@Entity
@Table(name="taxanomy")
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
public class Taxanomy implements Serializable {
...

父母:

@ManyToOne(fetch=FetchType.EAGER)
@Cascade({org.hibernate.annotations.CascadeType.ALL})
@JoinColumn(name="parent_id", nullable=true)
public Taxanomy getParent() {

...

孩子们:

@OneToMany(mappedBy="parent", fetch=FetchType.EAGER)
@Cascade({org.hibernate.annotations.CascadeType.ALL})
public Set<Taxanomy> getChildren() {

我使用findAll方法检索所有Taxanomy对象。这将返回所有对象树。根对象通过子关系包含所有其他的taxanomy对象。我希望列表中的所有对象,但在客户端,返回的列表包含2个根对象和其余数字(ids)。

taxanomy = [];
function getTaxanomy() {
  $.ajax({ url: 'taxanomy/all'
     , dataType : 'json'}).done(function(data)  {
         taxanomy = data;
     });
}


>> taxanomy
>> [>Object, 1001, >Object, 1005, 1005, 1003, 1007, 1006, 1002]
              ^              ^     ^     ^     ^     ^     ^
              |______________|_____|_____|_____|_____|_____|_ want real objects?

我如何在客户端强制生成所有对象而不是ID引用?我知道我可以迭代(或递归)树,并用ids

映射对象

1 个答案:

答案 0 :(得分:1)

尝试注释:

@JsonTypeInfo(use = JsonTypeInfo.Id.NONE)
class YourClassName{
   ....
}