iktuz
Oct 12th, 2011, 12:24 PM
Hi All,
When I transport Date as JSON reponse from server to the client page, the format is serialized as timestamp by default, like numeric value. I am trying to customize another format using a Date mask (with SimpleDateFormat). I was digging around a way to customize a global JSON Date response formatter. I dont want to use annotation for all date bean fields. All the suggestions I have followed around internet have failed. The most curious is that no exception is raised.
I am using:
Spring 3.0.6
jackson-core-asl-1.4.2 (** I have tried 1.8 and 1.9 as well)
jackson-mapper-asl-1.4.2 (** I have tried 1.8 and 1.9 as well)
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>My App</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<session-config>
<session-timeout>5</session-timeout>
</session-config>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/myApp-servlet.xml</param-value>
</context-param>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterPro xy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>myApp</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListe ner</listener-class>
</listener>
<servlet-mapping>
<servlet-name>myApp</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
</web-app>
Here is myApp-servlet.xml
<context:annotation-config />
<aop:aspectj-autoproxy proxy-target-class="true" />
<context:component-scan base-package="com.app" />
<mvc:annotation-driven />
<!-- Configures Handler Interceptors -->
<mvc:interceptors>
<!-- Changes the locale when a 'locale' request parameter is sent; e.g. /?locale=de -->
<bean class="org.springframework.web.servlet.i18n.LocaleChangeI nterceptor" />
</mvc:interceptors>
<!-- ... some mvc:view-controller declarations ... -->
<bean id="defaultViewResolver" class="org.springframework.web.servlet.view.UrlBasedViewR esolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/pages/" />
<property name="suffix" value=".jsp" />
</bean>
...
<!-- For JSON converter support -->
<bean class="org.springframework.web.servlet.mvc.annotation.Ann otationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<!-- Support JSON -->
<bean class="org.springframework.http.converter.json.MappingJac ksonHttpMessageConverter">
<property name="objectMapper" ref="jacksonDateMapper" />
</bean>
</property>
</bean>
<bean id="jacksonDateMapper" class="com.app.mapper.DateMapper">
<property name="mask" value="dd-MM-yyyy HH:mm" />
</bean>
...
And finally my DateMapper implementation:
package com.app.mapper;
import java.text.SimpleDateFormat;
import javax.annotation.PostConstruct;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig;
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);
//Using Jackson > 1.8 it's possible to invoke 'getSerializationConfig().withDateFormat(new SimpleDateFormat(mask));', doesnt work ;(
getSerializationConfig().setDateFormat(new SimpleDateFormat(mask));
}
public void setMask(String mask) {
this.mask = mask;
}
}
In the end, the date objects are still serialized as numbers, not following my formatter. That's it, can someone point some direction?
Thanks a lot,
Iktuz.
When I transport Date as JSON reponse from server to the client page, the format is serialized as timestamp by default, like numeric value. I am trying to customize another format using a Date mask (with SimpleDateFormat). I was digging around a way to customize a global JSON Date response formatter. I dont want to use annotation for all date bean fields. All the suggestions I have followed around internet have failed. The most curious is that no exception is raised.
I am using:
Spring 3.0.6
jackson-core-asl-1.4.2 (** I have tried 1.8 and 1.9 as well)
jackson-mapper-asl-1.4.2 (** I have tried 1.8 and 1.9 as well)
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>My App</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<session-config>
<session-timeout>5</session-timeout>
</session-config>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/myApp-servlet.xml</param-value>
</context-param>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterPro xy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>myApp</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListe ner</listener-class>
</listener>
<servlet-mapping>
<servlet-name>myApp</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
</web-app>
Here is myApp-servlet.xml
<context:annotation-config />
<aop:aspectj-autoproxy proxy-target-class="true" />
<context:component-scan base-package="com.app" />
<mvc:annotation-driven />
<!-- Configures Handler Interceptors -->
<mvc:interceptors>
<!-- Changes the locale when a 'locale' request parameter is sent; e.g. /?locale=de -->
<bean class="org.springframework.web.servlet.i18n.LocaleChangeI nterceptor" />
</mvc:interceptors>
<!-- ... some mvc:view-controller declarations ... -->
<bean id="defaultViewResolver" class="org.springframework.web.servlet.view.UrlBasedViewR esolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/pages/" />
<property name="suffix" value=".jsp" />
</bean>
...
<!-- For JSON converter support -->
<bean class="org.springframework.web.servlet.mvc.annotation.Ann otationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<!-- Support JSON -->
<bean class="org.springframework.http.converter.json.MappingJac ksonHttpMessageConverter">
<property name="objectMapper" ref="jacksonDateMapper" />
</bean>
</property>
</bean>
<bean id="jacksonDateMapper" class="com.app.mapper.DateMapper">
<property name="mask" value="dd-MM-yyyy HH:mm" />
</bean>
...
And finally my DateMapper implementation:
package com.app.mapper;
import java.text.SimpleDateFormat;
import javax.annotation.PostConstruct;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig;
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);
//Using Jackson > 1.8 it's possible to invoke 'getSerializationConfig().withDateFormat(new SimpleDateFormat(mask));', doesnt work ;(
getSerializationConfig().setDateFormat(new SimpleDateFormat(mask));
}
public void setMask(String mask) {
this.mask = mask;
}
}
In the end, the date objects are still serialized as numbers, not following my formatter. That's it, can someone point some direction?
Thanks a lot,
Iktuz.