2014-10-17 60 views
0

使自我参照对象使用Spring启动时如何配置杰克逊ObjectMapper在Spring的数据?配置杰克逊使用杰克逊的数据类型,休眠

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

我认为这个问题是一个序列化的问题,可以通过使用杰克逊的数据类型,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不会让我松了杰克逊ObjectMapper的默认行为?我不一定想失去这种行为,只是扩大它。此外,不会我需要把@Component注解的类才能有春天把它捡起来?

回答

1

如果您在使用Spring数据REST我想你需要春天启动1.2获得自动更新对象映射器(见here了解详细信息)。与旧版本你可以注入自己的,我认为(但你需要配置您需要的所有功能,如果它不仅仅是一个模块的更多)。