4

Spring 3.1 mvc を使用して、json を http メッセージとして使用して残りの Web サービスを生成することに成功しました。これまで、Bean のすべてのフィールドで、カスタム シリアライザー/デシリアライザーを使用する表記法を設定し、jackson に特定の形式で日付をフォーマットするように依頼していました。ここで、この表記構文を削除し、グローバルな日付形式を設定したいと思います。これが私がそれを作った方法です

これは私のサーブレット構成です

<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"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
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.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">

<context:component-scan base-package="com.test.endpoints" />

<mvc:annotation-driven>
    <mvc:message-converters register-defaults="false">
        <bean
            class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
            <property name="objectMapper">
                <bean class="com.test.CustomObjectMapper" />
            </property>
        </bean>
    </mvc:message-converters>
</mvc:annotation-driven>

</beans>

これはクラス CustomObjectMapper です

package com.test;

import java.text.SimpleDateFormat;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig.Feature;

public class CustomObjectMapper extends ObjectMapper {

    public CustomObjectMapper() {
        super();
        configure(Feature.WRITE_DATES_AS_TIMESTAMPS, false);
        setDateFormat(new SimpleDateFormat(
                "EEE MMM dd yyyy HH:mm:ss 'GMT'ZZZ (z)"));
    }
}
4

1 に答える 1

0

以下の xml を mvc 構成に追加して、日付のシリアル化をグローバルにオーバーライドします。

<mvc:annotation-driven > 
    <mvc:message-converters register-defaults="false">
        <bean     class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" >
            <property name="objectMapper">
                <bean class="package.CustomObjectMapper"/>
            </property>
        </bean>
    </mvc:message-converters>
 </mvc:annotation-driven>

以下のように CustomObjectMapper クラスを変更します。

import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig.Feature;

public class CustomObjectMapper extends ObjectMapper {
    public CustomObjectMapper(){
        super.configure(Feature.WRITE_DATES_AS_TIMESTAMPS, false);
    }
}
于 2012-09-12T08:54:09.167 に答える