10

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.

Rubis
  • 101
  • 1
  • 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; } ``` – Ilam Mar 14 '21 at 12:45
  • 1
    Does this answer your question? [Configure LocaldateTime in Spring Rest API](https://stackoverflow.com/questions/53365917/configure-localdatetime-in-spring-rest-api) – Simon Martinelli Mar 14 '21 at 13:17
  • Does this answer your question? [Spring Data JPA - ZonedDateTime format for json serialization](https://stackoverflow.com/questions/31627992/spring-data-jpa-zoneddatetime-format-for-json-serialization) – Michael Gantman Feb 15 '23 at 14:52

3 Answers3

3

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

1

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

  • 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. – NealeU May 22 '23 at 15:45
0

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

Sarvar Nishonboyev
  • 12,262
  • 10
  • 69
  • 70