Spring JPA: Jackson, OffsetDateTime object serialization
authorGustavo Martin Morcuende <gu.martinm@gmail.com>
Sun, 3 Jul 2016 15:36:56 +0000 (17:36 +0200)
committerGustavo Martin Morcuende <gu.martinm@gmail.com>
Sun, 3 Jul 2016 15:36:56 +0000 (17:36 +0200)
SpringJava/JPA/pom.xml
SpringJava/JPA/src/main/java/de/spring/persistence/converters/LocalDateTimeAttributeConverter.java [deleted file]
SpringJava/JPA/src/main/java/de/spring/persistence/converters/OffsetDateTimeAttributeConverter.java [new file with mode: 0644]
SpringJava/JPA/src/main/java/de/spring/persistence/example/domain/Ad.java

index 0794f92..aebecc9 100644 (file)
                <!--
                        Jackson JSON Processor, required by spring-webmvc. See messageConverters
                        in rest-config.xml
-               -->
+               
+                       Non required dependency. It is already declared in jackson-datatype-jsr310
                <dependency>
                        <groupId>com.fasterxml.jackson.core</groupId>
                        <artifactId>jackson-databind</artifactId>
                        <version>2.6.4</version>
                </dependency>
+               -->
                <!-- 
                        Jackson dependency required for serializing and deserializing LocalDateTime,
                        LocalDate, etc, etc objects.
diff --git a/SpringJava/JPA/src/main/java/de/spring/persistence/converters/LocalDateTimeAttributeConverter.java b/SpringJava/JPA/src/main/java/de/spring/persistence/converters/LocalDateTimeAttributeConverter.java
deleted file mode 100644 (file)
index 2d2259c..0000000
+++ /dev/null
@@ -1,22 +0,0 @@
-package de.spring.persistence.converters;
-
-import java.sql.Timestamp;
-import java.time.LocalDateTime;
-
-import javax.persistence.AttributeConverter;
-import javax.persistence.Converter;
-
-@Converter(autoApply = true)
-public class LocalDateTimeAttributeConverter implements AttributeConverter<LocalDateTime, Timestamp> {
-
-       @Override
-       public Timestamp convertToDatabaseColumn(LocalDateTime localDateTime) {
-               return (localDateTime == null ? null : Timestamp.valueOf(localDateTime));
-       }
-
-       @Override
-       public LocalDateTime convertToEntityAttribute(Timestamp sqlTimestamp) {
-               return (sqlTimestamp == null ? null : sqlTimestamp.toLocalDateTime());
-       }
-
-}
diff --git a/SpringJava/JPA/src/main/java/de/spring/persistence/converters/OffsetDateTimeAttributeConverter.java b/SpringJava/JPA/src/main/java/de/spring/persistence/converters/OffsetDateTimeAttributeConverter.java
new file mode 100644 (file)
index 0000000..3573265
--- /dev/null
@@ -0,0 +1,39 @@
+package de.spring.persistence.converters;
+
+import java.sql.Timestamp;
+import java.time.LocalDateTime;
+import java.time.OffsetDateTime;
+import java.time.ZoneId;
+import java.time.ZonedDateTime;
+
+import javax.persistence.AttributeConverter;
+import javax.persistence.Converter;
+
+@Converter(autoApply = true)
+public class OffsetDateTimeAttributeConverter implements AttributeConverter<OffsetDateTime, Timestamp> {
+
+       @Override
+       public Timestamp convertToDatabaseColumn(OffsetDateTime offsetDateTime) {
+               Timestamp timestamp = null;
+               
+               if (offsetDateTime != null) {
+                       timestamp = Timestamp.valueOf(offsetDateTime.toLocalDateTime());
+               }
+               
+               return timestamp;
+       }
+
+       @Override
+       public OffsetDateTime convertToEntityAttribute(Timestamp sqlTimestamp) {
+               OffsetDateTime offsetDateTime = null;
+               
+               if (sqlTimestamp != null) {
+                       final LocalDateTime localDateTime = sqlTimestamp.toLocalDateTime();
+                       final ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneId.systemDefault());
+                       offsetDateTime = zonedDateTime.toOffsetDateTime();
+               }
+               
+               return offsetDateTime;
+       }
+
+}
index be28d06..eb41328 100644 (file)
@@ -2,6 +2,7 @@ package de.spring.persistence.example.domain;
 
 import java.io.Serializable;
 import java.time.LocalDateTime;
+import java.time.OffsetDateTime;
 import java.util.Set;
 
 import javax.persistence.Column;
@@ -21,7 +22,9 @@ import javax.validation.constraints.Max;
 import javax.validation.constraints.NotNull;
 import javax.validation.constraints.Size;
 
-import de.spring.persistence.converters.LocalDateTimeAttributeConverter;
+import com.fasterxml.jackson.annotation.JsonFormat;
+
+import de.spring.persistence.converters.OffsetDateTimeAttributeConverter;
 
 @Entity
 @Table(name="ad", schema="mybatis_example")
@@ -77,14 +80,16 @@ public class Ad implements Serializable {
        private String adMobileImage;
 
        @NotNull
-       @Convert(converter=LocalDateTimeAttributeConverter.class)
+       @Convert(converter=OffsetDateTimeAttributeConverter.class)
        @Column(name="created_at", nullable=false)
-       private LocalDateTime createdAt;
+       @JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy-MM-dd HH:mm:ssZ")
+       private OffsetDateTime createdAt;
        
        @NotNull
-       @Convert(converter=LocalDateTimeAttributeConverter.class)
+       @Convert(converter=OffsetDateTimeAttributeConverter.class)
        @Column(name="updated_at", nullable = false)
-       private LocalDateTime updatedAt;
+       @JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy-MM-dd HH:mm:ssZ")
+       private OffsetDateTime updatedAt;
 
        // It will be used by JPA when filling the property fields with data coming from data base.
        protected Ad() {
@@ -92,8 +97,8 @@ public class Ad implements Serializable {
        }
 
        // It will be used by my code (for example by Unit Tests)
-       public Ad(Long id, Long companyId, Long companyCategId, String adMobileImage, LocalDateTime createdAt,
-                       LocalDateTime updatedAt) {
+       public Ad(Long id, Long companyId, Long companyCategId, String adMobileImage, OffsetDateTime createdAt,
+                       OffsetDateTime updatedAt) {
                this.id = id;
                this.companyCategId = companyCategId;
                this.adMobileImage = adMobileImage;
@@ -117,11 +122,11 @@ public class Ad implements Serializable {
                return adMobileImage;
        }
 
-       public LocalDateTime getCreatedAt() {
+       public OffsetDateTime getCreatedAt() {
                return createdAt;
        }
 
-       public LocalDateTime getUpdatedAt() {
+       public OffsetDateTime getUpdatedAt() {
                return updatedAt;
        }
 }