Spring 数据剩余:使用 LAZY 对象的实体序列化导致 JsonMappingException

Spring Data Rest: Entity serialization with LAZY object cause JsonMappingException

我在 Spring Data Rest 项目中遇到以下异常:

com.fasterxml.jackson.databind.JsonMappingException: 
    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: org.springframework.data.rest.webmvc.json.["content"]->test.spring.data.rest.xml.entities.Author_$$_jvstb93_1["handler"])

当然,我有一些实体具有获取配置 = FetchType.LAZY。

我遵循了很多说明和链接,但我仍然有这个异常。

我已经尝试过的(没有效果):

以上操作均未解决问题![​​=20=]

如何(肯定)解决这个问题?

谢谢。

我找到了解决这个恼人问题的方法。

对于 Spring Data Rest 应用程序的每个存储库,都必须定义一个自定义 @Projection;在投影中会有必要的字段。

请注意,如果两个实体之间存在循环引用,则投影的相关方法必须使用 @JsonBackReference 注释(对于 @ManyToOne 注释字段)和 @JsonManagedReference注解(对于@OneToMany注解字段),否则在JSON序列化中会有JSON循环。

在每个 @Repository 注释(或 @RepositoryRestResource 注释)中,必须使用自定义投影标记 excerptProjection 属性。

有了这个管理,就不需要任何其他配置了,Lazy对象的异常终于消失了。