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

Spring MVC how to handle Joda Data Types as JSON

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

@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 找到了一个帐户,return 如下所示

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

但是,当我保存一个日期为“2016-12-07 12:00:00”的帐户对象,然后尝试检索它时,我在客户端获得的日期是“1481108400000 “

所以我决定使用 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>

但我仍然遇到错误:与元素类型 "bean" 关联的属性 "p:indentOutput" 的前缀 "p" 未绑定

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

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

之后,Jackson 的 JodaModule 应该会自动格式化您的 JodaTime 类型。