Spring Data JPA - ZonedDateTime format for json serialization

I have a problem with the json serialization of ZonedDateTime. When converted to json it produces an enormous object and I don't want all that data to be transfered every time. So i tried to format it to ISO but it doesn't work. How can i get it to format?

Here is my Entity Class:

@MappedSuperclass
public abstract class AuditBase {

    @Id
    @GeneratedValue
    private Long id;

    @CreatedDate
    private ZonedDateTime createdDate;

    @LastModifiedDate
    private ZonedDateTime lastModifiedDate;

    @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
    public ZonedDateTime getLastModifiedDate() {
        return lastModifiedDate;
    }

    public void setLastModifiedDate(ZonedDateTime lastModifiedDate) {
        this.lastModifiedDate = lastModifiedDate;
    }

    @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
    public ZonedDateTime getCreatedDate() {
        return createdDate;
    }

    public void setCreatedDate(ZonedDateTime createdDate) {
        this.createdDate = createdDate;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @PrePersist
    public void prePersist() {
        this.createdDate = ZonedDateTime.now();
        this.lastModifiedDate = ZonedDateTime.now();
    }

    @PreUpdate
    public void preUpdate() {
        this.lastModifiedDate = ZonedDateTime.now();
    }
}

Solution 1:

I guess that you are using Jackson for json serialization, Jackson now has a module for Java 8 new date time API, https://github.com/FasterXML/jackson-datatype-jsr310.

Add this dependency into your pom.xml

<dependency>
    <groupId>com.fasterxml.jackson.datatype</groupId>
    <artifactId>jackson-datatype-jsr310</artifactId>
    <version>2.6.0</version>
</dependency>

And this is its usage:

 public static void main(String[] args) throws JsonProcessingException {
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.registerModule(new JavaTimeModule());
    System.out.println(objectMapper.writeValueAsString(new Entity()));
}

static class Entity {
    ZonedDateTime time = ZonedDateTime.now();

    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSZ")
    public ZonedDateTime getTime() {
        return time;
    }
}

The output is:

{"time":"2015-07-25T23:09:01.795+0700"}

Note : If your Jackson version is 2.4.x use

objectMapper.registerModule(new JSR310Module());

Hope this helps!

Solution 2:

Above answer works but if you don't want to touch your existing entity class, following settings will work with ZonedDateTime :

  public static ObjectMapper getMapper() {
    ObjectMapper mapper = new ObjectMapper(); 
    mapper.registerModule(new JavaTimeModule());    
    return mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, false)
.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS,false)
                        .configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS,false)
                        .configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false)
                        .setVisibility(mapper.getSerializationConfig()
                                .getDefaultVisibilityChecker()
                                .withFieldVisibility(JsonAutoDetect.Visibility.ANY)
                                .withGetterVisibility(JsonAutoDetect.Visibility.NONE)
                                .withSetterVisibility(JsonAutoDetect.Visibility.NONE)
                                .withCreatorVisibility(JsonAutoDetect.Visibility.NONE)); 
    }

Library:

<dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-csv</artifactId>
            <version>${jackson.dataformat.csv.version}</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.datatype</groupId>
            <artifactId>jackson-datatype-jsr310</artifactId>
        </dependency>

Solution 3:

I resolved the problem setting the following property to application.yml.

    spring:
      jackson:
        serialization:
          write_dates_as_timestamps: false

I use spring-boot:2.3.8.RELEASE and dependency-management:1.0.10.RELEASE.

For more detailed information see this link.