LocalDate is serialized as an array
Asked Answered
P

4

14

I'm using springBoot to develop a REST APi. And i have a LocalDate field "firstDate" in the response model of a GET endpoint. But this LocalDate is serializable as an array in the response's json!

"firstDate": [
        2021,
        3,
        1
      ],

So for consuming this APi, i have to define this date in my DTO as an array! which is not good! My reponse models of th API are generated with swagger so i can not use @JsonFormat(pattern="yyyy-MM-dd")

Can you help me please and tell me how to serialize LocalDate properly in this case ?

Thank you very much.

Parthenopaeus answered 14/3, 2021 at 12:24 Comment(3)
Have your tried this ``` @Bean public ObjectMapper objectMapper() { ObjectMapper mapper = new ObjectMapper(); mapper.registerModule(new JodaModule()); mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); return mapper; } ```Dormeuse
Does this answer your question? Configure LocaldateTime in Spring Rest APIPapen
Does this answer your question? Spring Data JPA - ZonedDateTime format for json serializationOutofdoors
H
6

My dates defined as LocalDateTime were been serializaded as an array like this:

"timestamp": [
    2023,
    2,
    15,
    10,
    30,
    45,
    732425200
],

So here is what I did in my WebConfig.java:

@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {

  // other configs

  @Override
  public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    WebMvcConfigurer.super.extendMessageConverters(converters);
    converters.add(new MappingJackson2HttpMessageConverter(
        new Jackson2ObjectMapperBuilder()
            .dateFormat(new StdDateFormat())
            .featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
            .build()));
  }

}

Now everything is good again:

"timestamp": "2023-02-15T10:32:06.5170689",

Hope it's been helpful. Some topics that helped me achieve that:

Configure LocaldateTime in Spring Rest API

How to customise the Jackson JSON mapper implicitly used by Spring Boot?

Can't serialize java.time.LocalDate as a String with Jackson

Haddad answered 15/2, 2023 at 14:26 Comment(0)
U
2

I think this WebConfig.class is unnecessary. You could try something like that in your field firstDate:

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")
private LocalDate firstDate;

There's no make sense define the field as an array in DTO because of the swagger config

Upright answered 15/2, 2023 at 14:58 Comment(1)
You don't seem to have understood the question. They are serialising a java LocalDate object, not an array. And it is bad practice to put repeated boilerplate like @JsonFormat on every date when those classes may need to be used in another application that talks a different format.Philippines
M
1

Setting deserializer and serializers for the date fields will solve the problem:

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

use LocalDateTimeSerializer for LocalDateTime fields

Mor answered 27/2, 2023 at 7:37 Comment(0)
S
-1

This is maybe is what you need. Create a customer ObjectMapper the snippet below objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

    @Configuration
    public class CustomMapper {
        @Bean
        public ObjectMapper objectMapper(){
            ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false)
//The line below will help fix your problem
            objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
            return objectMapper;
        }
Scope answered 18/6, 2024 at 12:30 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.