SpringMVC+Hibernate返回JSON数据有时会出现如下错误

在使用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的代理对象。该代理对象有些属性不能被序列化所以会报错。

解决办法:在类型上加如下注解把不需要序列化的属性屏蔽掉,在每一个bean上加上

@JsonIgnoreProperties(value = { "hibernateLazyInitializer""handler" })

亲测过,吧问题解决了


转至https://my.oschina.net/harmel/blog/529694

猜你喜欢

转载自blog.csdn.net/arryluo123/article/details/61429172
今日推荐