Last active
November 8, 2015 06:21
-
-
Save elucash/c1f926452dda6f3a8f24 to your computer and use it in GitHub Desktop.
how to get Include.NON_NULL working with Optionals
This file contains 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
package com.hotwire.containers; | |
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | |
import com.fasterxml.jackson.annotation.JsonInclude; | |
import com.fasterxml.jackson.annotation.JsonInclude.Include; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
import com.fasterxml.jackson.databind.annotation.JsonDeserialize; | |
import com.fasterxml.jackson.databind.annotation.JsonSerialize; | |
import com.fasterxml.jackson.datatype.guava.GuavaModule; | |
import com.google.common.base.Optional; | |
import java.io.IOException; | |
import org.immutables.value.Value; | |
@JsonIgnoreProperties(ignoreUnknown = true) | |
@JsonInclude(Include.NON_NULL) | |
@JsonSerialize(as = ImmutableUser.class) | |
@JsonDeserialize(as = ImmutableUser.class) | |
@Value.Immutable | |
public interface User { | |
Optional<Integer> id(); | |
Optional<String> login(); | |
Optional<String> name(); | |
Optional<String> email(); | |
Optional<String> bio(); | |
Optional<String> company(); | |
/* | |
<dependency> | |
<groupId>com.fasterxml.jackson.datatype</groupId> | |
<artifactId>jackson-datatype-guava</artifactId> | |
<version>2.5.2</version> | |
</dependency> | |
or | |
<dependency> | |
<groupId>com.fasterxml.jackson.datatype</groupId> | |
<artifactId>jackson-datatype-jdk8</artifactId> | |
<version>2.5.2</version> | |
</dependency> | |
depending which optional you use: Guava or Java 8 | |
*/ | |
public static void main(String... args) throws IOException { | |
ObjectMapper objectMapper = new ObjectMapper(); | |
objectMapper.registerModule(new GuavaModule()); // register module for Guava datatypes | |
objectMapper.registerModule(new Jdk8Module()); // register module for Jdk8 datatypes | |
ImmutableUser user = ImmutableUser.builder().email("test@test.com").build(); | |
System.out.println(objectMapper.writeValueAsString(user)); | |
// {"email":"test@test.com"} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Do you have any idea why :
The method registerModule(Module) in the type ObjectMapper is not applicable for the arguments (GuavaModule) is showing in my java class