问题
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