Created
November 7, 2016 06:06
-
-
Save RICH0423/e2e4a9521642d569b2bb33b5ab6aece4 to your computer and use it in GitHub Desktop.
Jersey Ignore Null Fields with Jackson
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @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... | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @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