Avoid Jackson serialization on non fetched lazy objects

前端 未结 14 2068
终归单人心
终归单人心 2020-11-22 13:02

I have a simple controller that return a User object, this user have a attribute coordinates that have the hibernate property FetchType.LAZY.

When I try to get this

14条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-22 13:33

    In the case of Spring Data Rest then, while the solution posted by @r1ckr works, all that is required is to add one of the following dependencies depending on your Hibernate version:

    
        com.fasterxml.jackson.datatype
        jackson-datatype-hibernate4
        ${jackson.version}
    
    

    or

    
        com.fasterxml.jackson.datatype
        jackson-datatype-hibernate5
        ${jackson.version}
    
    

    Within Spring Data Rest there is a class:

    org.springframework.data.rest.webmvc.json.Jackson2DatatypeHelper

    which will auto-detect and register the Module on application start-up.

    There is however an issue:

    Issue Serializing Lazy @ManyToOne

提交回复
热议问题