【发布时间】: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