15

JodaTime 処理用の Serializer を以下に示します。

public class JodaDateTimeJsonSerializer extends JsonSerializer<DateTime> {

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

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

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

        gen.writeString(formattedDate);
    }

}

次に、各モデル オブジェクトで、次のようにします。

@JsonSerialize(using=JodaDateTimeJsonSerializer.class )
public DateTime getEffectiveDate() {
    return effectiveDate;
}

上記の設定で@ResponseBody、Jackson Mapper は確実に動作します。しかし、私は書き続けるという考えが好きではありません@JsonSerialize。私が必要としているのは、@JsonSerializeモデル オブジェクトのないソリューションです。この構成を1つの構成としてSpring xmlのどこかに書くことは可能ですか?

あなたの助けに感謝。

4

5 に答える 5

11

各日付フィールドに注釈を付けることができますが、オブジェクト マッパーのグローバル構成を行うことをお勧めします。ジャクソンを使用する場合、次のようにスプリングを構成できます。

<bean id="jacksonObjectMapper" class="com.company.CustomObjectMapper" />

<bean id="jacksonSerializationConfig" class="org.codehaus.jackson.map.SerializationConfig"
    factory-bean="jacksonObjectMapper" factory-method="getSerializationConfig" >
</bean>

CustomObjectMapper の場合:

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)"));
    }
}

もちろん、SimpleDateFormat は必要な任意の形式を使用できます。

于 2012-05-31T13:59:00.090 に答える
1

@Moesioはほとんどそれを手に入れました。これが私の設定です:

<!-- Configures the @Controller programming model -->
<mvc:annotation-driven/>

<!-- Instantiation of the Default serializer in order to configure it -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapterConfigurer" init-method="init">
    <property name="messageConverters">
        <list>
            <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
                <property name="objectMapper" ref="jacksonObjectMapper" />
            </bean>
        </list>
    </property>
</bean>

<bean id="jacksonObjectMapper" class="My Custom ObjectMapper"/>

<bean id="jacksonSerializationConfig" class="org.codehaus.jackson.map.SerializationConfig"
    factory-bean="jacksonObjectMapper" factory-method="getSerializationConfig" />

私が得たビットは、<mvc:annotation-driven/>それが独自AnnotationMethodHandlerのものを作成し、手動で作成したものを無視することです。http://scottfrederick.blogspot.com/2011/03/customizing-spring-3-mvcannotation.htmlからBeanPostProcessing のアイデアを得て、使用されるものを構成しました。魅力のように機能します。

于 2013-02-16T01:48:26.077 に答える
0

Spring 3 の JavaConfig を使用して同じ:

@Configuration
@ComponentScan()
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter
{

    @Override
    public void configureMessageConverters(final List<HttpMessageConverter<?>> converters)
    {
        converters.add(0, jsonConverter());
    }

    @Bean
    public MappingJacksonHttpMessageConverter jsonConverter()
    {
        final MappingJacksonHttpMessageConverter converter = new MappingJacksonHttpMessageConverter();
        converter.setObjectMapper(new CustomObjectMapper());

        return converter;
    }
}
于 2014-01-30T13:46:17.727 に答える
-3

単にクラスパスに Jackson JAR があり、 を返す@ResponseBody場合、Spring は Model オブジェクトを自動的に JSON に変換します。これを機能させるためにモデルに注釈を付ける必要はありません。

于 2012-05-20T15:30:06.350 に答える