springboot集成JPA返回Json报错 com.fasterxml.jackson.databind.exc.InvalidDefinitionException

在使用JPA的getOne()方法时,SQl语句能够正常执行,但在数据绑定上出了错,报错如下:

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: com.example.entity.User$HibernateProxy$VLHIBAMd["hibernateLazyInitializer"])

报错的原因是:

在转化成json的时候,fasterxml.jackson将对象转换为json报错,发现有字段为null。

因为jsonplugin用的是java的内审机制,hibernate会给被管理的pojo加入一个hibernateLazyInitializer属性,jsonplugin会把

hibernateLazyInitializer也拿出来操作,并读取里面一个不能被反射操作的属性就产生了这个异常。

解决方法:

在实体类上加上如下注解:

@JsonIgnoreProperties(value = {"hibernateLazyInitializer"})

表示忽略hibernateLazyInitializer这个属性,那么也就不会出现为空的情况了。

原文地址:https://www.cnblogs.com/nieaojie625/p/13763522.html