Skip to content

Instantly share code, notes, and snippets.

@RICH0423
Created November 7, 2016 06:06
Show Gist options
  • Select an option

  • Save RICH0423/e2e4a9521642d569b2bb33b5ab6aece4 to your computer and use it in GitHub Desktop.

Select an option

Save RICH0423/e2e4a9521642d569b2bb33b5ab6aece4 to your computer and use it in GitHub Desktop.
Jersey Ignore Null Fields with Jackson
@JsonInclude(JsonInclude.Include.NON_NULL) // Class level
@Document(collection = "Project")
public class Project extends AbstractDocument {
private String name;
private String serialNumber;
private Double updatedAt;
@DBRef
private Customer customer;
@DBRef
private Universe universe;
// constructor, setter and getter...
}
@Provider
public class ObjectMapperContextResolver implements ContextResolver<ObjectMapper> {
private final ObjectMapper mapper;
public ObjectMapperContextResolver() {
mapper = new ObjectMapper();
// Ignore null value in JSON serialization
mapper.setSerializationInclusion(Include.NON_NULL);
}
@Override
public ObjectMapper getContext(Class<?> type) {
return mapper;
}
}
//Jersey config
//register(ObjectMapperContextResolver.class);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment