Home > Java > javaTutorial > How to Serialize ZonedDateTime in ISO Format with Spring Data JPA and Jackson?

How to Serialize ZonedDateTime in ISO Format with Spring Data JPA and Jackson?

DDD
Release: 2024-12-10 00:13:11
Original
419 people have browsed it

How to Serialize ZonedDateTime in ISO Format with Spring Data JPA and Jackson?

Spring Data JPA - Serializing ZonedDateTime as ISO Format

When working with ZonedDateTime in Spring Data JPA, its JSON serialization can lead to excessive data transfer. To mitigate this issue, let's explore how to format ZonedDateTime to ISO format for efficient serialization.

Solution Using Jackson Module for Java 8 Date Time API

To effectively handle ZonedDateTime serialization, install the Jackson module for Java 8 Date Time API.

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

Utilize the module as follows:

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(new JavaTimeModule());
Copy after login

Within your Entity class:

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

This will format the ZonedDateTime to ISO format during JSON serialization.

Note for Jackson Version 2.4.x

If you're using Jackson 2.4.x, replace the registration line with:

objectMapper.registerModule(new JSR310Module());
Copy after login

The above is the detailed content of How to Serialize ZonedDateTime in ISO Format with Spring Data JPA and Jackson?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template