在使用注解@ResponseBody返回由jpa(hibernate)API获取的存在关联属性的实体时,执行Jason转换时,会从数据库加载该关联的对象,而此时session已关闭,导致加载失败,进而导致Jason转换失败,会出现以下异常:
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); nested exception is com.fasterxml.jacks......
针对该异常一般有以下几种解决方式:
(1)在不影响数据完整性的情况下,使用set方法手动置空pojo类的Lazy关联对象。
(2)实体类的关联属性上使用@JsonIgnore注解。(这种方式会导致在其他使用关联属性对象做json转换的地方出问题)
(3)自定义类将需要向前台传递的关联对象的属性进行封装。