Reading Scott Fredericks's Blog I realized that I should bind the original Spring's AnnotationMethodHandlerAdapter instance with my customized ObjectMapper. In the first approach I was creating two instances and of course, mine wasn't be used, what explains why nothing happened. So the final configuration must be done as follows:
HTML Code:
<bean id="jacksonDateMapper" class="com.app.web.DateMapper">
<property name="mask" value="dd-MM-yyyy HH:mm" />
</bean>
<bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="objectMapper" ref="jacksonDateMapper" />
</bean>
<bean class="com.app.web.ConverterRegister">
<property name="messageConverters">
<list>
<ref bean="jsonConverter" />
</list>
</property>
</bean>
The class com.app.web.ConverterRegister is the binder:
Code:
package com.app.web;
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter;
@Component
public class ConverterRegister {
@Autowired
private AnnotationMethodHandlerAdapter adapter;
private HttpMessageConverter<?>[] messageConverters;
public void setMessageConverters(HttpMessageConverter<?>[] messageConverters) {
this.messageConverters = messageConverters;
}
@PostConstruct
public void bindMessageConverters() {
adapter.setMessageConverters(messageConverters);
}
}
The class DateMapper does the job, applying the format:
Code:
package com.app.web;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.annotation.PostConstruct;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.JsonSerializer;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig;
import org.codehaus.jackson.map.SerializerProvider;
import org.codehaus.jackson.map.ser.CustomSerializerFactory;
import org.springframework.stereotype.Component;
@Component("jacksonDateMapper")
public class DateMapper extends ObjectMapper {
private String mask = "MM-dd-yyyy HH:mm:ss";
@PostConstruct
public void afterPropertiesSet() throws Exception {
super.configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS, false);
//this one doesn't work at all, it's necessary to create and register a factory
//getSerializationConfig().withDateFormat(new SimpleDateFormat(mask));
//I am using Jackson 1.9 asl
CustomSerializerFactory factory = new CustomSerializerFactory();
factory.addSpecificMapping(Date.class, new JsonSerializer<Date>() {
@Override
public Class<Date> handledType() { return Date.class; }
@Override
public void serialize(Date value, JsonGenerator jgen,
SerializerProvider provider) throws IOException,
JsonProcessingException {
jgen.writeString(new SimpleDateFormat(mask).format(value));
}});
this.setSerializerFactory(factory);
}
public void setMask(String mask) {
this.mask = mask;
}
}
The factory exemple above I saw in another blog, sorry for the guy with the due credits. I apologize not having the URL. Well, in the end all the date instances are converted based on the specified format.
In the future I intend to use the user's locale to switch among many masks, any help will be very appreciated.
Thanks a lot,
Iktuz.