Jodatime/Spring MVC/Jackson | 日期时间格式问题

3
当我使用@ResponseData时,JodaTime被转换为其完整的对象状态,即使我已经使用了自定义序列化器。
配置:
Spring 3.1.2 Jackson 1.9.11
<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-core-asl</artifactId>
    <version>${jackson.version}</version>
</dependency>
<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-mapper-asl</artifactId>
    <version>${jackson.version}</version>
</dependency>

自定义序列化程序:

public class JodaDateTimeJsonSerializer extends JsonSerializer<DateTime> {

    //TODO Bad (hard) code. This should be part of a global system setting through ConfigurationService
    private static final String dateFormat = ("dd/MM/yyyy");

    private static Logger logger = LoggerFactory.getLogger(JodaDateTimeJsonSerializer.class);

    @Override
    public void serialize(DateTime date, JsonGenerator gen, SerializerProvider provider)
            throws IOException, JsonProcessingException {

        String formattedDate = DateTimeFormat.forPattern(dateFormat).print(date);
        logger.debug("Converted date string: {}", formattedDate);
        gen.writeString(formattedDate);
    }
}

调度程序:

 <mvc:annotation-driven />   

使用方法:

@JsonSerialize(using=JodaDateTimeJsonSerializer.class)
    public DateTime getExpiryDate() {
        return expiryDate;
    }

我得到的输出类似于这样:
"dateCreated":{"monthOfYear":12,"yearOfEra":2012,"yearOfCentury":12,"centuryOfEra":20,"millisOfSecond":359,"millisOfDay":53080359,"secondOfMinute":40,"secondOfDay":53080,"minuteOfHour":44,"minuteOfDay":884,"hourOfDay":14,"weekyear":2012,"weekOfWeekyear":51,"year":2012,"dayOfMonth":19,"dayOfWeek":3,"era":1,"dayOfYear":354,"chronology":{"zone":{"fixed":false,"cachable":false,"id":"Asia/Riyadh"}},"millis":1355917480359,"zone":{"fixed":false,"cachable":false,"id":"Asia/Riyadh"},"afterNow":false,"beforeNow":true,"equalNow":false},"dateModified":{"monthOfYear":12,"yearOfEra":2012,"yearOfCentury":12,"centuryOfEra":20,"millisOfSecond":359,"millisOfDay":53080359,"secondOfMinute":40,"secondOfDa

我需要一个简单的dd/mm/yyyy日期格式。

请给出建议。

此外,如何在全局设置此格式规则,以免每次都使用@JsonSerialize。

3个回答

3

这个链接对这种情况有所帮助。

基本上你需要为jackson提供一个序列化器和自定义的对象映射器。

序列化器:

public class JodaDateTimeJsonSerializer extends JsonSerializer<DateTime> {

    private static final String dateFormat = ("dd/MM/yyyy");

    private static Logger logger = LoggerFactory.getLogger(JodaDateTimeJsonSerializer.class);

    @Override
    public void serialize(DateTime date, JsonGenerator json, SerializerProvider provider)
            throws IOException, JsonProcessingException {

        String formattedDate = DateTimeFormat.forPattern(dateFormat).print(date);
        json.writeString(formattedDate);
    }
}

自定义对象映射器:

public class CustomJacksonObjectMapper extends ObjectMapper {

    public CustomJacksonObjectMapper(){
        CustomSerializerFactory factory = new CustomSerializerFactory();
        factory.addSpecificMapping(DateTime.class, new JodaDateTimeJsonSerializer());
        this.setSerializerFactory(factory);
    }

}

现在将自定义映射器注册到MVC。
<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"
    xmlns:context="http://www.springframework.org/schema/context"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:p="http://www.springframework.org/schema/p"
        xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
    <mvc:annotation-driven>
            <mvc:message-converters>
                <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
                    <property name="objectMapper" ref="customJacksonMapper" />
                </bean>
            </mvc:message-converters>
        </mvc:annotation-driven>   

这是可行的。


1

我会认为由于上述是jackson 2.0+,而原问题中提到的是jackson 1.9。 - Scott

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接