blaiso blaiso - 4 months ago 57
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:

@Entity
public class Account implements Serializable{
@Id
@GeneratedValue
private Long id;
@Temporal(TemporalType.TIMESTAMP)
private Date openinTime;
..........
}


and an entity Client with a Date field as followed

@Entity
public class Client {
@Id
@GeneratedValue
private Long id;
private String firstName;
private String lastName;
@Temporal(TemporalType.DATE)
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="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">

<mvc:default-servlet-handler />

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

<!-- i18n -->
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="WEB-INF/i18n" />
<property name="defaultEncoding" value="UTF-8" />
<property name="useCodeAsDefaultMessage" value="true" />
</bean>
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.FixedLocaleResolver">
<property name="defaultLocale" value="en" />
</bean>

<!-- View Handler -->
<bean
class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="favorPathExtension" value="true" />
<property name="mediaTypes">
<map>
<entry key="xml" value="text/xml" />
<entry key="json" value="application/json" />
<entry key="html" value="text/html" />
<entry key="less" value="text/html" />
</map>
</property>
<property name="viewResolvers">
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/" />
<property name="suffix" value=".jsp" />
</bean>
</property>
</bean>

<bean id="objectMapper"
class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean"
p:indentOutput="true" p:simpleDateFormat="yyyy-MM-dd HH:mm:ss">
</bean>

<bean
class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"
p:targetObject-ref="objectMapper" p:targetMethod="registerModule">
<property name="arguments">
<list>
<bean class="com.fasterxml.jackson.datatype.joda.JodaModule" />
</list>
</property>
</bean>

<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter" />
<bean
class="org.springframework.http.converter.ResourceHttpMessageConverter" />
<bean
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper" ref="objectMapper" />
</bean>
<bean
class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter" />
</mvc:message-converters>
</mvc:annotation-driven>




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

Answer

You must declare the p XML namespace as xmlns:p="http://www.springframework.org/schema/p" in your <beans> element:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    ... <other namespaces> ...
>

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

Comments