Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LocalDate Serialization: date as array?

I use Java 11 and want to serialize/deserialize LocalDate/LocalDateTime as String. Okay. I added dependency:

    <dependency>
        <groupId>com.fasterxml.jackson.datatype</groupId>
        <artifactId>jackson-datatype-jsr310</artifactId>
        <version>${jackson.version}</version>
    </dependency>

and module:

@Bean
public ObjectMapper objectMapper() {
    return new ObjectMapper()
            .registerModule(new JavaTimeModule())
            .configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
            .enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
}

When I send date to my app, it deserializes correctly:

{"profileId":12608,"birthDate":"2008-03-20","relativeType":"SON","cohabitants":true}

When I using objectMapper as bean, directly, it serializes correctly too:

{"code":"SUCCESS","id":868,"profileId":12608,"birthDate":"2008-03-20","relativeType":"SON","cohabitants":true}

But when it serializes with controller, it serializes as array:

{"code":"SUCCESS","id":868,"profileId":12608,"birthDate":[2008,3,20],"relativeType":"SON","cohabitants":true}

Problem is to deserialize date in body on controller. Controller is:

@PostMapping
public Relative create(@Validated(Validation.Create.class) @RequestBody Relative relative) {
    return service.create(relative);
}

Relative.class:

@Getter
@Setter
@ToString(callSuper = true)
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class Relative extends MortgageResponse {

    @Null(groups = Validation.Create.class)
    @NotNull(groups = Validation.Update.class)
    private Long id;

    @NotNull
    private Long profileId;

    private LocalDate birthDate;
    private RelativeType relativeType;
    private Boolean cohabitants;
}

Please, advice me, what's problem and how to fix it.

like image 392
Вячеслав Чернышов Avatar asked Mar 30 '20 07:03

Вячеслав Чернышов


People also ask

Is LocalDate serializable?

Note that LocalDate implements Serializable.

How does Jackson deserialize dates from JSON?

How to deserialize Date from JSON using Jackson. In order to correct deserialize a Date field, you need to do two things: 1) Create a custom deserializer by extending StdDeserializer<T> class and override its deserialize(JsonParser jsonparser, DeserializationContext context) method.

Does LocalDate extend date?

No, you cannot extend LocalDate, so that your implementation is a LocalDate in your environment.

What is the default value of LocalDate?

1. LocalDate. LocalDate is an immutable class that represents Date with default format of yyyy-MM-dd.

What is the default serialization and deserialization of date in Java?

The default serialization and deserialization of java.util.Date and other date-time types such as java.time.LocalDateTime is done in numeric formats. The Date is converted to the long type and other types are converted to string with number values.

How to use localdate over date in Spring Boot?

LocalDate came with Java 8 and is part of the new standard API in Java for working with dates. However, if you want to effectively use LocalDate over Date in a Spring Boot application, you need to take some extra care, since not all tools support LocalDate by default, yet. Spring Boot includes the popular Jackson library as JSON (de-)serializer.

What is the default date format of default Gson localdateserializer?

1. Custom GSON LocalDateSerializer Note that we are formatting default local date "2018-10-26" to "27-Oct-2018". 2. Custom GSON LocalDateTimeSerializer

How do I use the localdate module with Jackson objectmapper?

You can register the module with a Jackson ObjectMapper instance like this: The module teaches the ObjectMapper how to work with LocalDate s and the parameter WRITE_DATES_AS_TIMESTAMPS tells the mapper to represent a Date as a String in JSON.


1 Answers

Add the @JsonFormat annotation to your birthDate field , or rather any date field and your ObjectMapper (Spring Boot or not) should respect the formatting, as long as you have the additional js310 dependency on your classpath.

@JsonFormat(pattern="yyyy-MM-dd")
private LocalDate birthDate;
like image 97
Marco Behler Avatar answered Oct 12 '22 16:10

Marco Behler