Cannot serialize java.time.LocalDate as string using Jackson

I am using spring-boot 1.2.1.RELEASE with jackson 2.6.2, including the jsr310 data type. I am using the @SpringBootApplication annotation to run my Spring application. I have

spring.jackson.serialization.write_dates_as_timestamps = false

installed in my application application.properties (which, as I know, is being read because I tested it with banner = false).

And yet java.time.LocalDate is still serializing as an array of integers. I do not use @EnableWebMvc.

It looks like if I add a tag

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

into my LocalDate variable, then it works. But I thought it was automatic with the above set of properties. Plus, if I remember correctly (since then I just decided to work with the whole array), which worked only with serialization and not with deserialization (but I can’t honestly remember whether this last part is true).

+4
source share
3 answers

This is to know the problem in Spring Boot. You need to do it manually.

objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);

or upgrade to 1.2.2.

UPDATE: There is also a configuration methodObjectMapper used by Spring from your container.

+6
source

... , , (--).

, @Configuration () @Primary ... , , :

@Configuration
public class WebConfigurer {

    @Bean
    @Primary
    public ObjectMapper objectMapper(Jackson2ObjectMapperBuilder builder) {
        ObjectMapper objectMapper = builder.build();
        objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
        return objectMapper;
    }

}

, , JSR 310 pom.xml.

0

LocalDateTime , [2019,10,14,15,7,6,31000000]. 1.5.13.RELEASE. JavaTimeModule ObjectMapper, . ObjectMapper, :

@Bean
  public ObjectMapper getObjectMapper() {
    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
    mapper.registerModule(new JavaTimeModule());
    return mapper;
  }
0
source

Source: https://habr.com/ru/post/1610292/


All Articles