jackson 2 object to json ignore lazy loading

半城伤御伤魂 提交于 2019-12-08 04:28:20

问题


I am using jackson 2 to parse an object to json format I am having problem when I parse lazy loading fields. I want to get a null reference if the object cannot be loaded.

How can I do that?

I want to discuss with you a solution and I want to know if there is a problem when I use it?

I have found that during parsing jackson uses getters method to get attributes So my solution is to modify the getters methods of lazy loading fields suppose that post is a lazy loaded field this is the getter of that fields

public Collection<Post> getPosts() {
        try{
            posts.size();
            return posts;
        }
        catch (Exception e){

        }
        return null;
    }

This solution is working fine but I don't know if it will cause future problems or not. We should also use @JsonInclude(JsonInclude.Include.NON_NULL) to ignore null fields.

来源:https://stackoverflow.com/questions/45022685/jackson-2-object-to-json-ignore-lazy-loading

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!