3

I have found this two:

Jackson Object Mapper in spring MVC not working

why is the Object Mapper date fromat not used by message converters for date transform?

but none are the solution for me.

Spring MVC xml:

<mvc:annotation-driven>
    <mvc:message-converters register-defaults="false">
        <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
          <property name="objectMapper">
             <bean class="com.xxx.model.MyJacksonObjectMapper">

             </bean>
          </property>
        </bean>
    </mvc:message-converters>
</mvc:annotation-driven>

MyJacksonObjectMapper:

public class MyJacksonObjectMapper extends ObjectMapper {

    private static final long serialVersionUID = -3282890427623599460L;

    public MyJacksonObjectMapper() {
        super();
        disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
        setDateFormat(new SimpleDateFormat("yyyy-MM-dd"));          
    }

}

Not matter I change register-defaults to true or false, it is still not working, my controller always serialize the object's date value in timestamp (long) format.

My POJO object is not appended with any annotation, it is plain POJO and I cannot annotate it with any annotation (not my class), I believe there must be config to allow serialize default object's date in desired format, but it is not working, please help.

Dependency:

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.7.3</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.7.3</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>4.2.5.RELEASE</version>
</dependency>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Sam YC
  • 10,725
  • 19
  • 102
  • 158

3 Answers3

3

OK this answer https://stackoverflow.com/a/7714503/1542363 indirectly saved me, although I don't know why.

It mentioned this should be removed:

<mvc:annotation-driven/>

My issue is similar to this, I have duplicated annotation block in Spring config XML:

so I change my config from:

<mvc:annotation-driven>
  <mvc:argument-resolvers>
    <bean class="org.springframework.security.web.method.annotation.AuthenticationPrincipalArgumentResolver" />
  </mvc:argument-resolvers>
</mvc:annotation-driven>

<mvc:annotation-driven>
    <mvc:message-converters register-defaults="false">
        <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
          <property name="objectMapper">
             <bean class="com.xxx.model.MyJacksonObjectMapper">

             </bean>
          </property>
        </bean>
    </mvc:message-converters>
</mvc:annotation-driven>

to:

<mvc:annotation-driven>
    <mvc:argument-resolvers>
        <bean class="org.springframework.security.web.method.annotation.AuthenticationPrincipalArgumentResolver" />
    </mvc:argument-resolvers>
    <mvc:message-converters register-defaults="false">
        <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
          <property name="objectMapper">
             <bean class="com.xxx.model.MyJacksonObjectMapper"></bean>
          </property>
        </bean>
    </mvc:message-converters>
</mvc:annotation-driven>

then it works, not sure what is the reason.

Sam YC
  • 10,725
  • 19
  • 102
  • 158
2

I had the same issue, but didn't want to write a custom object mapper. I found the a post on www.armedia.com which showed how to configure this without writing a custom mapper. Using Spring 4.3.14 and Jackson 2.9.2, the following xml config is working for me. I believe it should work for earlier versions of Spring and Jackson.

<!-- set JSON date format to ISO-8601 e.g. 1970-01-01T00:00:00.000+0000 -->
<bean id="sourceObjectMapper" class="com.fasterxml.jackson.databind.ObjectMapper" />
<bean id="acmObjectMapper"
    class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
    <property name="targetObject" ref="sourceObjectMapper" />
    <property name="targetMethod" value="disable" />
    <property name="arguments" value="WRITE_DATES_AS_TIMESTAMPS" />
</bean>
<bean id="acmJacksonConverter"
    class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
    <property name="objectMapper" ref="acmObjectMapper" />
</bean>

<!-- We configure the Jackson mapper to output dates in ISO801 format. This requires adding 
     our customized Jackson mapper to the list of Spring MVC message converters. But, if 
     we just add our bean here all by itself, it will handle requests it should not handle,  
     e.g. encoding strings.  So we need to add the other standard message converters here 
     too, and make sure to put the customized Jackson converter AFTER the string converter. 
-->
<mvc:annotation-driven>
    <mvc:message-converters>
        <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/>
        <bean class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter"/>
        <bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
        <bean class="org.springframework.http.converter.ResourceHttpMessageConverter"/>
        <bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter"/>
        <ref bean="acmJacksonConverter"/>
        <bean class="org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter"/>
        <bean class="org.springframework.http.converter.BufferedImageHttpMessageConverter"/>
        <bean class="org.springframework.http.converter.FormHttpMessageConverter"/>
        <bean class="org.springframework.http.converter.xml.Jaxb2CollectionHttpMessageConverter"/>
    </mvc:message-converters>
</mvc:annotation-driven>

All credit should go to dmiller at: https://www.armedia.com/blog/spring-mvc-setting-json-date-format-2/

Tim Perry
  • 84
  • 1
  • 11
1

Maybe you have 2 labels in your xml configure file, and such as:

<mvc:message-converters register-defaults="false">
    <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
      <property name="objectMapper">
         <bean class="com.xxx.model.MyJacksonObjectMapper">

         </bean>
      </property>
    </bean>
</mvc:message-converters>

..... ..... .....

<mvc:annotation-driven/>

As I guess that the last label will overwrite the first one, so your MyJacksonObjectMapper class will not take effect.

liviaerxin
  • 579
  • 6
  • 13