【问题标题】:Configuring Jackson to enable self-referencing objects using jackson-datatype-hibernate使用 jackson-datatype-hibernate 配置 Jackson 以启用自引用对象
【发布时间】:2014-12-13 05:51:46
【问题描述】:

使用 Spring Boot 时如何在 Spring Data 中配置 Jackson ObjectMapper?

当我尝试返回自引用实体时,当我尝试在自引用表中检索对象的子对象时,我不断收到 "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" 错误。

我认为这个问题是一个序列化问题,可以通过使用 jackson-datatype-hibernate 包帮助我序列化自引用对象来解决。 https://github.com/FasterXML/jackson-datatype-hibernate

到目前为止,我还没有看到任何关于如何配置 Spring Data Rest 使用的现有 ObjectMapper 实例的明确指导。

根据项目的 GitHub 页面,我需要做的就是继承 ObjectMapper

public class HibernateAwareObjectMapper extends ObjectMapper {

    public HibernateAwareObjectMapper() {
        registerModule(new Hibernate4Module());
    }
}

但是,如果我将 ObjectMapper 子类化,我不会失去 Jackson ObjectMapper 的默认行为吗?我不一定想失去这种行为,只是扩展它。另外,我不需要在类上添加@Component 注释以便让 Spring 拾取它吗?

【问题讨论】:

    标签: spring-boot spring-data-rest


    【解决方案1】:

    如果您使用的是 Spring Data REST,我认为您需要 Spring Boot 1.2 才能自动更新对象映射器(有关详细信息,请参阅 here)。我认为对于旧版本,您可以注入自己的(但您确实需要配置您需要的所有功能,如果它不仅仅是一个模块)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-23
      • 2016-10-02
      • 2017-09-29
      相关资源
      最近更新 更多