1.出错地方
使用SpringBoot JPA获取的对象返回前端时报错:Debug的时候在Controller以下都没有问题(整个类中所有属性都有值),在返回前端的Debug时,发现报错在ServletInvocableHandlerMethod中,处理返回值时报错:
随后,查看此时的数据和异常信息:
2.异常信息
下面是主要的异常信息:
2019-07-28 21:45:39.117 ERROR 21008 --- [nio-8803-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet
[dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.http.converter.HttpMessageConversionException:
Type definition error:
[simple type, class org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor];
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:
com.hand.area.entity.Country$HibernateProxy$FHGzOBvp["hibernateLazyInitializer"])]
with root cause
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.hand.area.entity.Country$HibernateProxy$FHGzOBvp["hibernateLazyInitializer"])
at com.fasterxml.jackson.databind.exc.InvalidDefinitionException
.from(InvalidDefinitionException.java:77) ~[jackson-databind-2.9.9.jar:2.9.9]
3.解决办法
在需要返回的类添加注解:
@JsonIgnoreProperties({"hibernateLazyInitializer","handler"})
4.总结
从注解中理解是Hibernate懒加载初始化器,具体也不太清楚,对应的解决办法我是参考:参考链接。里面也没有太具体的说明