blaiso blaiso - 2 months ago 36x
Java Question

Spring MVC how to handle Joda Data Types as JSON

I'm starting a new spring mvc project. I'm using JPA for my entities mapping.
I had an entity name Account with a dateTime field annotated as followed:

public class Account implements Serializable{
private Long id;
private Date openinTime;

and an entity Client with a Date field as followed

public class Client {
private Long id;
private String firstName;
private String lastName;
private Date birthDate;


in my Spring-mvc controller, i find an account by id and return it as followed

@RequestMapping(value = "/find", method = RequestMethod.GET, produces = "application/json")
public ResponseEntity<?> findAccountById(@RequestParam("accountId") Long accountId) {
Account accountFound = accountService.findById(accountId);
ResponseEntity<Account> responseEntity = new ResponseEntity<Account>(accountFound, HttpStatus.OK);
return responseEntity;

but, when i saved an account object with date like "2016-12-07 12:00:00", and then, try to retrieve it, the date i'm getting on the client side is "1481108400000"

So i decide to use jodatime instead of java.util.Date (hoping that it will solve my problem)

I have configure in my spring-mvc xml dispatcher file the dateFormatter. this is my xml file:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
xmlns:xsi="" xmlns:mvc=""

<mvc:default-servlet-handler />

<!-- Login Interceptor -->
<mvc:mapping path="/protected/**" />
<bean class="softbank.ui.interceptor.LoginInterceptor" />
<!-- workaround to fix IE8 problem -->
<bean id="webContentInterceptor"
<property name="cacheSeconds" value="0" />
<property name="useExpiresHeader" value="true" />
<property name="useCacheControlHeader" value="true" />
<property name="useCacheControlNoStore" value="true" />

<!-- i18n -->
<bean id="messageSource"
<property name="basename" value="WEB-INF/i18n" />
<property name="defaultEncoding" value="UTF-8" />
<property name="useCodeAsDefaultMessage" value="true" />
<bean id="localeResolver"
<property name="defaultLocale" value="en" />

<!-- View Handler -->
<property name="favorPathExtension" value="true" />
<property name="mediaTypes">
<entry key="xml" value="text/xml" />
<entry key="json" value="application/json" />
<entry key="html" value="text/html" />
<entry key="less" value="text/html" />
<property name="viewResolvers">
<property name="prefix" value="/" />
<property name="suffix" value=".jsp" />

<bean id="objectMapper"
p:indentOutput="true" p:simpleDateFormat="yyyy-MM-dd HH:mm:ss">

p:targetObject-ref="objectMapper" p:targetMethod="registerModule">
<property name="arguments">
<bean class="com.fasterxml.jackson.datatype.joda.JodaModule" />

<bean class="org.springframework.http.converter.StringHttpMessageConverter" />
class="org.springframework.http.converter.ResourceHttpMessageConverter" />
<property name="objectMapper" ref="objectMapper" />
class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter" />

but i'm still getting and error : The prefix "p" for attribute "p:indentOutput" associated with an element type "bean" is not bound


You must declare the p XML namespace as xmlns:p="" in your <beans> element:

<beans xmlns=""
    ... <other namespaces> ...

After that, the JodaModule for Jackson should automatically format your JodaTime types.