Avoid Jackson serialization on non fetched lazy objects

前端 未结 14 2005
终归单人心
终归单人心 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:25

    Although this question is slightly different to this one : Strange Jackson exception being thrown when serializing Hibernate object, the underlying problem can be fixed in the same way with this code:

    @Provider
    public class MyJacksonJsonProvider extends JacksonJsonProvider {
        public MyJacksonJsonProvider() {
            ObjectMapper mapper = new ObjectMapper();
            mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
            setMapper(mapper);
        }
    }
    

提交回复
热议问题