在使用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" })
来源:oschina
链接:https://my.oschina.net/u/2427367/blog/529694