8

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.

SAMUEL
  • 8,098
  • 3
  • 42
  • 42
  • 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 – stacker Nov 18 '18 at 20:12
  • 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? – JB Nizet Nov 18 '18 at 20:18
  • 8
    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. – teppic Nov 18 '18 at 22:36
  • @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. – Martin Jes Rasmussen Nov 19 '18 at 06:32
  • @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. :) – Martin Jes Rasmussen Nov 19 '18 at 07:00
  • @teppic If you create a "proper" response, I will accept that as the answer, since it was your comment that got me on the right track. – Martin Jes Rasmussen Nov 19 '18 at 14:04
  • @MartinJesRasmussen: just tick your self answer. If you're interested, there are some other questions with the same cause that have answers with more detail (like [this](https://stackoverflow.com/a/51065174/3591528)). – teppic Nov 19 '18 at 20:50

3 Answers3

16

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.

  • Thanks! In the absence of ‘@EnableWebMVC' I found this also occurs if you’re manually initialising MockMVC in your tests. e.g., MockMvcBuilders.standaloneSetup(controller)... Using ‘@AutoConfigureMockMvc' with an autowired MockMvc fixes this. – ldeck Feb 02 '21 at 17:32
3

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.

SAMUEL
  • 8,098
  • 3
  • 42
  • 42
0

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>
Zomzog
  • 1
  • 2
  • Looks helpful. It’s not my home field, but I was under the impression that it’s been replaced by [jackson-modules-java8](https://github.com/FasterXML/jackson-modules-java8)? The link does contain dependencies for for Spring. – Ole V.V. Nov 19 '18 at 05:12
  • 1
    Should have written, that I already tried that. Also, I can see that jsr310 is indeed included as a transient dependency. – Martin Jes Rasmussen Nov 19 '18 at 06:29