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.
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>
Utilize the module as follows:
ObjectMapper objectMapper = new ObjectMapper(); objectMapper.registerModule(new JavaTimeModule());
Within your Entity class:
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSZ") public ZonedDateTime getTime() { return time; }
This will format the ZonedDateTime to ISO format during JSON serialization.
If you're using Jackson 2.4.x, replace the registration line with:
objectMapper.registerModule(new JSR310Module());
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!