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
Release: 2024-12-17 17:35:14
Original
578 people have browsed it

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;
Copy after login

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>
    Copy after login
  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;
        }  
    }
    Copy after login
  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;
        }
    }
    Copy after login

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
Copy after login

Expected Response:

{"birthDate":"2015-03-01"}
Copy after login
2015-03-01
Copy after login

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!

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