2

I cannot get jackson to serialize dates correctly.

It always serialize as:

{"now":[2016,7,1,15,41,44,753]}

Instead of:

{"now":"2016-07-01T15:41:44.753"}

This is my source:

src/main/java/br/com/jjw/spring/jackson/bug/App.java

package br.com.jjw.spring.jackson.bug;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class App {

    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }

}

src/main/java/br/com/jjw/spring/jackson/bug/MvcConfig.java

package br.com.jjw.spring.jackson.bug;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

@Configuration
public class MvcConfig extends WebMvcConfigurationSupport {

    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations("classpath:/public/");
    }

}

src/main/java/br/com/jjw/spring/jackson/bug/MyRestController.java

package br.com.jjw.spring.jackson.bug;

import java.util.Collections;
import java.util.Map;

import org.joda.time.LocalDateTime;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyRestController {

    @RequestMapping("/get")
    public Map<String, Object> get() {
        return Collections.singletonMap("now", new LocalDateTime());
    }

}

src/main/resources/application.properties

server.port=3000
spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS=false
spring.jackson.date-format=yyyy-MM-dd'T'HH:mm:ss.SSS

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>br.com.jjw</groupId>
    <artifactId>spring-jackson-bug</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.5.RELEASE</version>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </dependency>

        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.datatype</groupId>
            <artifactId>jackson-datatype-joda</artifactId>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
Beto Neto
  • 3,962
  • 7
  • 47
  • 81
  • I think I found the source of the problem, on `MappingJackson2HttpMessageConverter` it uses `Jackson2ObjectMapperBuilder.json().build()` ignoring the `application.properties`. This is fired when spring instantiate `RequestMappingHandlerAdapter` creating internaly a `AllEncompassingFormHttpMessageConverter` – Beto Neto Jul 01 '16 at 19:04

1 Answers1

1

I found a solution, I just changed my MvcConfig to extends WebMvcConfigurerAdapter.

package br.com.jjw.spring.jackson.bug;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
public class MvcConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations("classpath:/public/");
    }

}

To add, this post can help to understand why Difference between WebMvcConfigurationSupport and WebMvcConfigurerAdapter

Community
  • 1
  • 1
Beto Neto
  • 3,962
  • 7
  • 47
  • 81