Spring Boot JPA Entity Jackson序列化触发懒加载的解决方案

2,116 阅读2分钟

Spring Jpa这项技术在Spring 开发中经常用到。

今天在做项目用到了Entity的关联懒加载,但是在返回Json的时候,不管关联数据有没有被加载,都会触发数据序列化,而如果关联关系没有被加载,此时是一个HibernateProxy,并不是真实的数据,而导致了报错。

例如这个Topic Entity:

@Entity
@Table(name = "yms_topics")
@Getter
@Setter
@NoArgsConstructor
@EntityListeners(AuditingEntityListener.class)
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@NamedEntityGraphs({
        @NamedEntityGraph(name = "topic.all",
                attributeNodes = {
                        @NamedAttributeNode(value = "author"),
                        @NamedAttributeNode(value = "category")
                })
})
public class Topic implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @ManyToOne(targetEntity = User.class, cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @JoinColumn(name = "user_id")
    private User author;

    @ManyToOne(targetEntity = TopicCategory.class, cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @JoinColumn(name = "category_id")
    private TopicCategory category;

    @Column(nullable = false, length = 200)
    private String title;

    @Lob
    @Column(nullable = false, length = 50000)
    private String content;

    @CreatedDate
    private Date createdAt;

    @LastModifiedDate
    private Date updatedAt;
}

author 和 category 都是多对一的关联,也就是作者和分类,定义的是懒加载LAZY,现在需要分页取出记录,Repository 如下:

@EntityGraph(value = "topic.all")
Page<Topic> findAll(Pageable pageable);

这是关联读取author和category数据,没有任何问题。但是如果有的关联不需要加载,将EntityGraph去掉,就会报错。

Page<Topic> findAll(Pageable pageable);

究其原因就是HibernateProxy 没有办法被序列化,网上有很多的方法,例如JsonIgnoreProperties,这是治标不治本的方法

现在要达到的目标是当有关联数据的时候序列化,不存在的时候不返回,或者直接返回Null。

其实要解决这个问题很简单,那就是使用 Jackson 的一个包 jackson-datatype-hibernate5。 首先gradle添加依赖:

compile group: 'com.fasterxml.jackson.datatype', name: 'jackson-datatype-hibernate5', version: '2.9.8'

这个版本要注意jackson-datatype-hibernateX,根据Hibernate的版本来定

然后我们要重写 SpringMvc的 MappingJackson2HttpMessageConverter,将Hibernate5Module这个Module 注册到ObjectMapper

我们新建一个WebMvcConfig类,如下:

@Configuration
public class WebMvcConfig {

    @Bean
    public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        ObjectMapper mapper = converter.getObjectMapper();
        Hibernate5Module hibernate5Module = new Hibernate5Module();
        mapper.registerModule(hibernate5Module);
        mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        return converter;
    }
}

这是一个Config类,很简单

  • 就是注入一个Bean,类型为MappingJackson2HttpMessageConverter,获取到ObjectMapper
  • 通过mapper.registerModule(hibernate5Module);注册Module
  • 还可以定义时间如期的序列化格式。
  • 注意如果要让未加载的时候完全不输出,那么在Entity的类级别注解要使用Empty,例如:@JsonInclude(JsonInclude.Include.NON_EMPTY),不然当数据为null的时候会输出null。

到这里我们就可以达到预期的目的了。

这里可能会导致spring.jackson的配置失效,以后再行研究。