Home >Java >javaTutorial >How to Properly Format Java 8's LocalDate with Jackson?

How to Properly Format Java 8's LocalDate with Jackson?

Patricia Arquette
Patricia ArquetteOriginal
2024-12-17 17:35:14639browse

How to Properly Format Java 8's LocalDate with Jackson?

Using Jackson to Format LocalDate in Java 8

In this article, we'll explore how to format java.time.LocalDate using Jackson in Java 8.

Problem Description

When working with Java 8's LocalDate field, attempting to use the same annotations used for java.util.Date may not yield the desired results.

Failed Attempt

@JsonDeserialize(using = LocalDateDeserializer.class)  
@JsonSerialize(using = LocalDateSerializer.class)  
private LocalDate dateOfBirth;

Solution

To correctly format LocalDate, the following steps should be taken:

  1. Add the jackson-datatype-jsr310 dependency:

    <dependency>
        <groupId>com.fasterxml.jackson.datatype</groupId>
        <artifactId>jackson-datatype-jsr310</artifactId>
        <version>2.4.0</version>
    </dependency>
  2. Create a ContextResolver for ObjectMapper:

    import com.fasterxml.jackson.databind.ObjectMapper;
    import com.fasterxml.jackson.databind.SerializationFeature;
    import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
    import javax.ws.rs.ext.ContextResolver;
    import javax.ws.rs.ext.Provider;
    
    @Provider
    public class ObjectMapperContextResolver implements ContextResolver<ObjectMapper> {  
        private final ObjectMapper MAPPER;
    
        public ObjectMapperContextResolver() {
            MAPPER = new ObjectMapper();
            MAPPER.registerModule(new JavaTimeModule());
            MAPPER.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
        }
    
        @Override
        public ObjectMapper getContext(Class<?> type) {
            return MAPPER;
        }  
    }
  3. Register the ObjectMapperContextResolver in the resource class:

    @Path("person")
    public class LocalDateResource {
    
        @GET
        @Produces(MediaType.APPLICATION_JSON)
        public Response getPerson() {
            Person person = new Person();
            person.birthDate = LocalDate.now();
            return Response.ok(person).build();
        }
    
        @POST
        @Consumes(MediaType.APPLICATION_JSON)
        public Response createPerson(Person person) {
            return Response.ok(
                    DateTimeFormatter.ISO_DATE.format(person.birthDate)).build();
        }
    
        public static class Person {
            public LocalDate birthDate;
        }
    }

Testing

You can now test the formatting by issuing the following HTTP requests:

# Get person
curl -v http://localhost:8080/api/person

# Create person
curl -v -POST -H "Content-Type:application/json" -d "{\"birthDate\":\"2015-03-01\"}" http://localhost:8080/api/person

Expected Response:

{"birthDate":"2015-03-01"}
2015-03-01

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

Statement:
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