5

I am using Jackson JSON in a Spring 3 MVC app. To not serialize each and every single Date field, I created a custom objectmapper that uses a specific DateFormat:

@Component("jacksonObjectMapper")
public class CustomObjectMapper extends ObjectMapper
{
    Logger log = Logger.getLogger(CustomObjectMapper.class);

    @PostConstruct
    public void afterProps()
    {
        log.info("PostConstruct... RUNNING");
        //ISO 8601
        getSerializationConfig().setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SZ"));


    }

    //constructors...

}

This custom ObjectMapper is injected into the JsonConverter:

<bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
    <property name="supportedMediaTypes" value="application/json" />
    <property name="objectMapper" ref="jacksonObjectMapper" /> <!-- defined in CustomObjectMapper -->
</bean>

There is no exception in the logs and serialization works, but it is not picking up the dateformat, it simple serializes to a timestamp. The @PostConstruct annotation works, the log statement in the method is in the logs.

Does anyone know why this fails?

Robby Pond
  • 73,164
  • 16
  • 126
  • 119
Sven Haiges
  • 2,636
  • 5
  • 42
  • 54

3 Answers3

3

You may also need to specify that you want textual Date serialization, by doing:

configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS, false);

(although I was assuming setting non-null date format might also trigger it, but maybe not)

Also, you can do configuration of mapper directly from constructor (which is safe). Not that it should change behavior, but would remove need for separate configuration method.

StaxMan
  • 113,358
  • 34
  • 211
  • 239
  • Sorry, all that did not seem to help. Very strange. I still see the date printed out as a timestamp. – Sven Haiges Mar 10 '11 at 13:25
  • Definitely something odd going then; sounds like maybe either you are not configuring ObjectMapper that is being used, or Jackson is not being used for serialization. – StaxMan Mar 10 '11 at 18:22
0

I've done the below which works to get around compatability with Java / PHP timestamps. Java uses milliseconds since EPOCH and PHP uses seconds so was simpler to use ISO dates.

I declare the below message adapters:

<bean id="messageAdapter"
    class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="messageConverters">
        <list>
            <bean id="jacksonJsonMessageConvertor"
                class="my.app.MyMappingJacksonHttpMessageConverter"/>                   
        </list>
    </property>
</bean>

And MyMappingJacksonHttpMessageConverter looks like the below:

public class MyMappingJacksonHttpMessageConverter extends MappingJacksonHttpMessageConverter {

public MyMappingJacksonHttpMessageConverter(){
    super();
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.configure(Feature.WRITE_DATES_AS_TIMESTAMPS, false);
    setObjectMapper(objectMapper);
}
}

With the above all dates are written out in ISO format.

0

For Spring config application.properties

spring.jackson.serialization.fail-on-empty-beans=false
Dmitry
  • 812
  • 8
  • 13