2

I am trying to serialise java.time.LocalDateTime to JSON format via Jackson 2.6.2 library.

My maven dependencies is:

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>${jackson.version}</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    <version>${jackson.version}</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>${jackson.version}</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.datatype</groupId>
    <artifactId>jackson-datatype-jsr310</artifactId>
    <version>${jackson.version}</version>
</dependency>

My spring configuration is:

<mvc:annotation-driven>
    <mvc:message-converters>
        <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
            <property name="objectMapper" ref="objectMapper"/>
        </bean>
    </mvc:message-converters>
</mvc:annotation-driven>
<bean id="objectMapper" class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">
    <property name="indentOutput" value="false"/>
    <property name="simpleDateFormat" value="yyyy-MM-dd HH:mm:ss.SSS"/>
</bean>

But the output JSON is:

"2015-09-15T21:00:01.562"

Please help to figure out, thanks a lot.

  • 1
    What are you expecting for a timestamp? The output reflects what is in your objectMapper bean... – ryekayo Oct 02 '15 at 14:59
  • If the simpleDateFormat would be applied correctly then the output should be *2015-09-15 21:00:01.562*; so it seems that the pattern used in date formatting is `yyyy-MM-dd'T'HH:mm:ss.SSS`; Could you check your codebase for this particular sequence of text `'T'` see if it comes up? – Filip Oct 02 '15 at 15:21
  • You should also pay attention to the documentation of [Jackson2ObjectMapperFactoryBean](http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/http/converter/json/Jackson2ObjectMapperFactoryBean.html#setSimpleDateFormat-java.lang.String-) it states that: *Note: Setting this property makes the exposed ObjectMapper non-thread-safe, according to Jackson's thread safety rules.* – Filip Oct 02 '15 at 15:23
  • You should have a look at this answer as well [add-a-jackson-objectmapper-dateformat](http://stackoverflow.com/questions/13098971/how-do-i-add-a-jackson-objectmapper-dateformat-config-into-spring-mvc-config) – Filip Oct 02 '15 at 15:28
  • Or this answer [jackson2-json-iso-8601-date...](http://stackoverflow.com/questions/13700853/jackson2-json-iso-8601-date-from-jodatime-in-spring-3-2rc1) since it also uses `org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean` – Filip Oct 02 '15 at 15:30

0 Answers0