Spring MVC如何将Joda数据类型作为JSON

时间:2016-07-12 11:39:23

标签: java spring-mvc jpa jackson jodatime

我正在开始一个新的春季mvc项目。我使用JPA进行实体映射。 我有一个实体名称Account,其日期时间字段注释如下:

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

以及具有日期字段的实体客户端

@Entity
public class Client {
    @Id
    @GeneratedValue
    private Long id;
    private String firstName;
    private String lastName;
    @Temporal(TemporalType.DATE)
    private Date birthDate;

}

在我的Spring-mvc控制器中,我按ID找到一个帐户并按照

返回
@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;
}

但是,当我保存一个帐户对象,其日期类似于&#34; 2016-12-07 12:00:00&#34;,然后,尝试检索它,我正在进入的日期客户端是&#34; 1481108400000&#34;

所以我决定使用jodatime而不是java.util.Date(希望它能解决我的问题)

我在spring-mvc xml调度程序文件中配置了dateFormatter。这是我的xml文件:

<?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>

但我仍然得到并且错误:前缀&#34; p&#34; for attribute&#34; p:indentOutput&#34;与元素类型相关联&#34; bean&#34;不受约束

1 个答案:

答案 0 :(得分:0)

您必须在p元素中将xmlns:p="http://www.springframework.org/schema/p" XML命名空间声明为<beans>

<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> ...
>

之后,杰出的JodaModule应该自动格式化你的JodaTime类型。