在使用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"])java
还有...........cn.harmel.lms.system.domain.XXX_$$_jvst988_0["hibernateLazyInitializer"])dom
这是由于你须要序列化对象有一个属性是一类类型,而你使用了Hibernate的延迟加载因此这里是个Hibernate的代理对象。该代理对象有些属性不能被序列化因此会报错。hibernate
解决办法:在类型上加以下注解把不须要序列化的属性屏蔽掉代理
@JsonIgnoreProperties(value = { "hibernateLazyInitializer", "handler" })