Last active
May 30, 2016 19:11
-
-
Save hugomarques/eddb46a4d43f457c9af6a1a3949b8a77 to your computer and use it in GitHub Desktop.
Java8-Jackson Problem
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
| import com.fasterxml.jackson.databind.ObjectMapper; | |
| import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; | |
| import lombok.Getter; | |
| import lombok.Setter; | |
| import lombok.ToString; | |
| import java.io.IOException; | |
| import java.time.Instant; | |
| import java.time.LocalDate; | |
| import java.time.LocalDateTime; | |
| /** | |
| * Created by hugomarques on 5/25/16. | |
| */ | |
| @ToString @Getter @Setter | |
| public class CreatureJava8 { | |
| private String name; | |
| private Integer age; | |
| private LocalDate birthday; | |
| private LocalDateTime createdAt; | |
| private Instant createdAtInstant; | |
| public static void main(String[] args) throws IOException { | |
| final String creatureJson = "{\"name\": \"dragon\", \"age\": \"3100\", \"birthday\": \"2010-08-14\"," + | |
| "\"createdAt\": \"2010-08-14T13:10:05Z\", \"createdAtInstant\": \"2010-08-14T13:10:05Z\"}"; | |
| ObjectMapper objectMapper = new ObjectMapper(); | |
| JavaTimeModule javaTimeModule = new JavaTimeModule(); | |
| objectMapper.registerModule(javaTimeModule); | |
| CreatureJava8 creature = objectMapper.readValue(creatureJson, CreatureJava8.class); | |
| System.out.println(creature); | |
| } | |
| } |
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
| import com.fasterxml.jackson.databind.ObjectMapper; | |
| import lombok.Getter; | |
| import lombok.Setter; | |
| import lombok.ToString; | |
| import java.io.IOException; | |
| import java.time.Instant; | |
| import java.time.LocalDate; | |
| import java.time.LocalDateTime; | |
| /** | |
| * Created by hugomarques on 5/25/16. | |
| */ | |
| @ToString @Getter @Setter | |
| public class CreatureJava8Problem { | |
| private String name; | |
| private Integer age; | |
| private LocalDate birthday; | |
| private LocalDateTime createdAt; | |
| private Instant createdAtInstant; | |
| public static void main(String[] args) throws IOException { | |
| final String creatureJson = "{\"name\": \"dragon\", \"age\": \"3100\", \"birthday\": \"2010-08-14\"," + | |
| "\"createdAt\": \"2010-08-14 13:10:05\", \"createdAtInstant\": \"2010-08-14 13:10:05\"}"; | |
| ObjectMapper objectMapper = new ObjectMapper(); | |
| CreatureJava8Problem creature = objectMapper.readValue(creatureJson, CreatureJava8Problem.class); | |
| System.out.println(creature); | |
| } | |
| } |
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
| package com.hugodesmarques.catalog.common.config; | |
| import com.fasterxml.jackson.databind.ObjectMapper; | |
| import com.fasterxml.jackson.databind.SerializationFeature; | |
| import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; | |
| import org.springframework.context.annotation.Bean; | |
| import org.springframework.context.annotation.Configuration; | |
| /** | |
| * Created by hugomarques on 4/26/16. | |
| */ | |
| @Configuration | |
| public class JSONConfig { | |
| @Bean | |
| public ObjectMapper objectMapper() { | |
| ObjectMapper objectMapper = new ObjectMapper(); | |
| objectMapper.registerModule(new JavaTimeModule()); | |
| objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); | |
| return objectMapper; | |
| } | |
| } |
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
| package com.hugodesmarques.catalog.common.utils; | |
| import com.fasterxml.jackson.core.JsonProcessingException; | |
| import com.fasterxml.jackson.databind.ObjectMapper; | |
| import java.io.ByteArrayInputStream; | |
| import java.io.IOException; | |
| import java.io.InputStream; | |
| /** | |
| * Created by hugomarques on 4/7/16. | |
| */ | |
| @Component | |
| public class JSONUtils { | |
| @Autowired | |
| private static ObjectMapper jsonObjectMapper; | |
| } |
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
| <?xml version="1.0" encoding="UTF-8"?> | |
| <project xmlns="http://maven.apache.org/POM/4.0.0" | |
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
| <modelVersion>4.0.0</modelVersion> | |
| <groupId>com.hugodesmarques</groupId> | |
| <artifactId>jackson-java8-example</artifactId> | |
| <version>1.0-SNAPSHOT</version> | |
| <build> | |
| <plugins> | |
| <plugin> | |
| <groupId>org.apache.maven.plugins</groupId> | |
| <artifactId>maven-compiler-plugin</artifactId> | |
| <version>3.5.1</version> | |
| <configuration> | |
| <source>1.8</source> | |
| <target>1.8</target> | |
| </configuration> | |
| </plugin> | |
| </plugins> | |
| </build> | |
| <dependencies> | |
| <dependency> | |
| <groupId>com.fasterxml.jackson.core</groupId> | |
| <artifactId>jackson-databind</artifactId> | |
| <version>2.6.5</version> | |
| </dependency> | |
| <dependency> | |
| <groupId>org.projectlombok</groupId> | |
| <artifactId>lombok</artifactId> | |
| <version>1.16.4</version> | |
| </dependency> | |
| </dependencies> | |
| </project> |
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
| <?xml version="1.0" encoding="UTF-8"?> | |
| <project xmlns="http://maven.apache.org/POM/4.0.0" | |
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
| <modelVersion>4.0.0</modelVersion> | |
| <groupId>com.hugodesmarques</groupId> | |
| <artifactId>jackson-java8-example</artifactId> | |
| <version>1.0-SNAPSHOT</version> | |
| <build> | |
| <plugins> | |
| <plugin> | |
| <groupId>org.apache.maven.plugins</groupId> | |
| <artifactId>maven-compiler-plugin</artifactId> | |
| <version>3.5.1</version> | |
| <configuration> | |
| <source>1.8</source> | |
| <target>1.8</target> | |
| </configuration> | |
| </plugin> | |
| </plugins> | |
| </build> | |
| <dependencies> | |
| <dependency> | |
| <groupId>com.fasterxml.jackson.core</groupId> | |
| <artifactId>jackson-databind</artifactId> | |
| <version>2.6.5</version> | |
| </dependency> | |
| <dependency> | |
| <groupId>com.fasterxml.jackson.datatype</groupId> | |
| <artifactId>jackson-datatype-jsr310</artifactId> | |
| <version>LATEST</version> | |
| </dependency> | |
| <dependency> | |
| <groupId>org.projectlombok</groupId> | |
| <artifactId>lombok</artifactId> | |
| <version>1.16.4</version> | |
| </dependency> | |
| </dependencies> | |
| </project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment