Spring Boot 2 not serializing LocalDateTime

spring jackson serialization: write-dates-as-timestamps
jsonformat localdatetime
com fasterxml jackson-databind exc mismatchedinputexception expected array or string
jackson deserialize zoneddatetime
spring boot localdatetime converter
spring.jackson.date-format iso8601
jackson zoneddatetime
javatimemodule

I recently tried to implement a micro service using spring-boot 2.

Now, whenever I attempt to return an object which contains a java.time.LocalDateTime from my REST service, the LocalDateTime get serialized as an array of integers. Like so:

{
  "id": "5bf1425f9f8de267f04b22ad",
  "description": "aaaaaarrrgggghhhhh",
  "timestamp": [
    2018,
    11,
    18,
    11,
    43,
    43,
    889000000
  ],
  "time": 2.25,
  ...
}

I have tried configuring the ObjectMapper through settings in application.yml

spring:
  jackson:
    serialization:
      write-dates-as-timestamps: false

but doesn't work. I have also tried configuring a new ObjectMapper through a Spring Configuration class, like so:

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

My configuration gets loaded (debugger stops at a breakpoint) - it's just that it does nothing.

I tried adding jackson dependencies manually (also for the jsr310 module) to my pom.xml - also without any luck.

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
</dependency>

For some reason it looks like Spring Boot is ignoring my attempts to anything with the ObjectMapper, and it keeps returning the same result.

Setting log level to DEBUG for com.fasterxml in the application.yml also yields no output:

logging:
  level:
    com.fasterxml: DEBUG

I use Spring Boot 2.1.0-RELEASE with Jackson 2.9.7.

The basic pom file was generated from https://start.spring.io My project compiles for and runs on a Java 8 JVM.

This answer is based on teppic's comment to the original post.

The issue was caused by @EnableWebMVC on one of my @Configuration classes. Removed @EnableWebMVC immediately solved the problem.

JSON Java 8 LocalDateTime format in Spring Boot, update: Spring Boot 2.x doesn't require this configuration anymore. spring.​jackson.serialization.write_dates_as_timestamps=false to all LocalDateTime datatype and you do not have to specify @JsonFormat above every LocalDateTime  Problems with Spring Boot when serializing java.time.LocalDateTime with Jackson to return JSON ISO-8601 timestamps? I'm working on converting some models in a spring-boot REST API app to use java 8's java.time.LocalDateTime instead of joda's DateTime. I want the timestamps returned from API call to adhere to the ISO_8601 format.

From your comments I found the issue as you blend the @EnableWebMvc with sprongboot. It turns out that Spring Boot doesn’t mix well with the standard Spring MVC @EnableWebMvc. What happens when you add the annotation is that spring boot autoconfiguration is disabled.

Serializing LocalDate to JSON in Spring Boot, 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  Today, I stumbled (once again) over LocalDate in a Spring Boot application.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,

You must add jsr310 support to jackson :

<dependency>
    <groupId>com.fasterxml.jackson.datatype</groupId>
    <artifactId>jackson-datatype-jsr310</artifactId>
    <version>2.4.0</version>
</dependency>

Jackson Date, Date, then Joda-Time as well as the Java 8 DateTime. 2. Serialize Date to Timestamp. First – let's see how to serialize a simple java.util. Serializing to this terse timestamp format is not optimal. I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2:. spring.jackson.serialization.write_dates_as_timestamps=false This will ensure that a correct converter is used, and dates will be printed in the format of 2016-03-16T13:56:39.492 Annotations are only needed in case you want to change the date format.

Formatting JSON Dates in Spring Boot, Explore a number of different ways to format JSON dates in a Spring Boot application. We're not hiring Java-only authors right now. Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2: Java 8 date types, like LocalDate and LocalDateTime – we can use it only to format  In this blogpost, I’m going to show a small example application which should hopefully help in formatting Java 8 dates in a Spring Boot app. To start, let’s head to start.spring.io and create a starter project. I choose a project based on Gradle, Java and Spring Boot 2.1.2, but if you prefer Maven, that will work too.

Spring Boot LocalDate field serialization and deserialization, With this, all my LocalDate fields automatically use the ISO format, no need to In my Spring Boot 2 applications #JsonFormat annotation is used in REST  Step 2 - Adds serialization. First things first. We have to add JSR-310 module. It is a datatype module to make Jackson recognize Java 8 Date & Time API data types. Note that in this example jackson-datatype-jsr310 version is inherited from spring-boot-dependencies dependency management. Dependency in pom.xml

Serialization of Spring Boot and Feign using Java 8 Time and Date , However, when we use Spring Boot or Spring Cloud Feign, we often find problems when we LocalDate: no suitable constructor found, can not deserialize from Object value Spring Booot 2.0 features are being serialized. I am using Springboot 2.0.6 and for some reason the app yml changes did not work. And also I had more requirements. I tried creating ObjectMapper and marking it as Primary but spring boot complained that I already have jacksonObjectMapper as marked Primary!! So this is what I did. I made changes to the internal mapper.

Comments
  • did you try to create an ObjectMapper instead of passing a builder to method args and then build a new one based on it? mapper = new ObjectMapper() and then customize it
  • Just created a demo project with spring-initializr, (Java 8, Maven, Web support). I ddn't do anything other than add a controller and a simple class with a LocalDateTime property, and I can't reproduce. Are you sure you don't have other dependencies that would change the configuration of Jackson?
  • Do you have @EnableWebMvc on one of your @Configuration classes? If so, remove it. It fights with the spring boot way of configuring spring mvc.
  • @JBNizet I think you may be on to something. For this project I'm using mongodb - it may be that the spring-starter-mongo serializer/deserializer is the culprit - I'll get back when I have tried to remove it.
  • @teppic +1 Bingo that was the problem - now I hope my Cors config still works... Too bad I can't accept your response as a solution. :)
  • Looks helpful. It’s not my home field, but I was under the impression that it’s been replaced by jackson-modules-java8? The link does contain dependencies for for Spring.
  • Should have written, that I already tried that. Also, I can see that jsr310 is indeed included as a transient dependency.