杰克逊没有反序列化域类的List属性

时间:2014-08-23 16:48:02

标签: java json jackson

我有一个Category域模型对象,正在为Angular应用构建一个REST风格的后端接口。目前,Category对象创建和持久性按预期工作,但categoryList的内部属性在所有GET请求上返回null。

我的Category型号:

@Entity
@NamedQuery(
  name="findCategoryByName",
  query="select category FROM Category c WHERE c.category = :category"
)
@Table(name = "categories")
public class Category implements Serializable{

  @Id
  @Column(name = "category_id")
  @GeneratedValue(strategy = GenerationType.AUTO)
  private int id;

  @Column
  private String category;

  @ManyToMany(mappedBy = "categories", cascade = CascadeType.PERSIST)
  private List<Photo> photos;

  @ManyToOne(cascade = CascadeType.PERSIST)
  private Category parent;

  @OneToMany(mappedBy = "parent")
  @JsonDeserialize(as = ArrayList.class, contentAs = Category.class)
  private List<Category> categoryList;

  public Category() { }

}

AppConfig.java中我的杰克逊配置:

public MappingJackson2HttpMessageConverter jacksonMessageConverter(){
    MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
    ObjectMapper mapper = new ObjectMapper();
    mapper.registerModule(new Hibernate4Module());
    converter.setObjectMapper(mapper);

    return converter;
}

@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converterList){
    converterList.add(jacksonMessageConverter());
    super.configureMessageConverters(converterList);
}

我的CategoryRepository实施中通过此方法检索数据:

@Override
public List<Category> findAll(){
    List<Category> results = getEntityManager().createQuery("from Category").getResultList();
    return results;
}

当我向localhost:8080/admin/categories发送GET请求时,我得到了

{
    "id": 42,
    "category": "newCategory",
    "photos": null,
    "parent": {
        "id": 41,
        "category": "testCategory",
        "photos": null,
        "parent": null,
        "categoryList": null
    },
    "categoryList": null
}

但是,如果我在return results;方法实现中的FindAll()上设置了断点,我可以看到CategorycategoryList字段的长度为1并包含对newCategory类别的引用。

为什么此列表值不会出现在GET请求中?

0 个答案:

没有答案