报错详情
org.springframework.data.redis.serializer.SerializationException: Could not write JSON: No serializer found for class org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain:
xx.xx.xx.xx.xx.XXX$HibernateProxy$KnxukjCm["$$_hibernate_interceptor"]); nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain:
xx.xx.xx.xx.xx.XXX$HibernateProxy$KnxukjCm["$$_hibernate_interceptor"])
Caused by: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain:
xx.xx.xx.xx.xx.XXX$HibernateProxy$KnxukjCm["$$_hibernate_interceptor"])
问题分析
①条件:
使用了JPA的Repository的getOne()方法。并且使用了Redis对其进行缓存。
②过程:
getOne()方法是一个延迟加载操作。只能获得对实体的引用(代理),实际上没有进行数据库访问。只有当调用它的属性时才会查询数据库。
解决方案① (推荐)
使用findById().get()方法来获取一个真实的实体。
解决方案②
在延时加载相关的字段上或者在实体类上加入相关注解忽略
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
解决方案③
或者在延时加载的字段上修改延时加载属性
FetchType.LAZY
改为
FetchType.EAGER
表示急加载立即加载