Home > Java > javaTutorial > How to Properly Format Java 8 LocalDate Objects in JSON using Jackson?

How to Properly Format Java 8 LocalDate Objects in JSON using Jackson?

Mary-Kate Olsen
Release: 2024-12-30 01:54:08
Original
373 people have browsed it

How to Properly Format Java 8 LocalDate Objects in JSON using Jackson?

Formatting Java 8 LocalDate with Jackson

Problem:

In Java applications, converting Date objects into JSON format with specific date patterns can be straightforward using the @JsonFormat annotation. However, when dealing with java.time.LocalDate introduced in Java 8, the same approach may not work as expected.

Proposed Solution:

To customize the formatting of LocalDate objects for JSON serialization, the following approach can be used:

  1. Configure Jackson ObjectMapper:
  2. Create a ContextResolver implementation for ObjectMapper.
  3. Register the JavaTimeModule in the ObjectMapper to support LocalDate serialization.
  4. Enable the WRITE_DATES_AS_TIMESTAMPS feature to false, as LocalDate represents dates without timestamps.
  5. Use ContextResolver in JAX-RS Application:
  6. Define a JAX-RS service class with @Provider annotation.
  7. Implement the ObjectMapperContextResolver interface to provide the configured ObjectMapper.
  8. Annotate Resource Class for JSON Conversion:
  9. Add the @JsonDeserialize and @JsonSerialize annotations with proper deserializer and serializer classes to handle LocalDate conversion.

Example Code:

ContextResolver:

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;

@Provider
public class ObjectMapperContextResolver implements ContextResolver<ObjectMapper> {
    @Override
    public ObjectMapper getContext(Class<?> type) {
        ObjectMapper mapper = new ObjectMapper();
        mapper.registerModule(new JavaTimeModule());
        mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
        return mapper;
    }
}
Copy after login

Resource Class:

import java.time.LocalDate;

@Path("person")
public class LocalDateResource {
    @GET
    public Person getPerson() {
        return new Person(LocalDate.now());
    }
}
Copy after login

Person Class:

public class Person {
    @JsonDeserialize(using = LocalDateDeserializer.class)
    @JsonSerialize(using = LocalDateSerializer.class)
    private LocalDate birthDate;
}
Copy after login

By implementing this approach, LocalDate objects can be serialized and deserialized effectively, enabling flexible JSON formatting.

The above is the detailed content of How to Properly Format Java 8 LocalDate Objects in JSON using Jackson?. For more information, please follow other related articles on the PHP Chinese website!

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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template