SpringMVC+Hibernate返回JSON數據報No serializer found錯誤

在使用SpringMVC+Hibernate環境中返回JSON數據有時會出現如下錯誤:Handler execution resulted in exception: Could not write content: No serializer found for class org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) ) (through reference chain:.................cn.harmel.lms.system.domain.XXX_$$_jvst988_0["handler"])

還有...........cn.harmel.lms.system.domain.XXX_$$_jvst988_0["hibernateLazyInitializer"])

這是因爲你需要序列化對象有一個屬性是一類類型,而你使用了Hibernate的延遲加載所以這裏是個Hibernate的代理對象。該代理對象有些屬性不能被序列化所以會報錯。

解決辦法:在類型上加如下註解把不需要序列化的屬性屏蔽掉

@JsonIgnoreProperties(value = { "hibernateLazyInitializer""handler" })
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章