java - Spring MVC how to handle Joda Data Types as JSON -


i'm starting new spring mvc project. i'm using jpa entities mapping. had entity name account datetime field annotated followed:

@entity public class account implements serializable{     @id     @generatedvalue     private long id;     @temporal(temporaltype.timestamp)     private date openintime;     .......... } 

and entity client date field followed

@entity public class client {     @id     @generatedvalue     private long id;     private string firstname;     private string lastname;     @temporal(temporaltype.date)     private date birthdate; 

}

in spring-mvc controller, find account id , return 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 saved account object date "2016-12-07 12:00:00", , then, try retrieve it, date i'm getting on client side "1481108400000"

so decide use jodatime instead of java.util.date (hoping solve problem)

i have configure in spring-mvc xml dispatcher file dateformatter. 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 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 , error : prefix "p" attribute "p:indentoutput" associated element type "bean" not bound

you must declare p xml namespace xmlns:p="http://www.springframework.org/schema/p" in <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, jodamodule jackson should automatically format jodatime types.


Comments

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -