1

私は、Bean のセットアップに純粋な Java 構成を使用する Spring WebMvc (Spring Boot ではない) プロジェクトに取り組んでいます。LocalDateTime などの java.time (jsr310) オブジェクトで @DateTimeFormat アノテーションを尊重するように Spring/Jackson を取得するのに苦労しています。

基本的な webmvc アプリケーション spring-context および spring-webmvc (バージョン 4.3.0.RELEASE) に関連する spring jar とともに、クラスパスに jackson-datatype-jsr310 および jackson-databind jar (バージョン 2.7.4) の両方があります。

関連する構成クラスは次のとおりです。

@Configuration
@ComponentScan({"com.example.myapp"})
public class WebAppConfig extends WebMvcConfigurationSupport {
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        ObjectMapper mapper = Jackson2ObjectMapperBuilder
            .json()
            .indentOutput(true)
            .featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
            .findModulesViaServiceLoader(true)
            .build();

        converters.add(new MappingJackson2HttpMessageConverter(mapper));

        super.addDefaultHttpMessageConverters(converters);
    }
}

これは、残りのコントローラーでデータ モデルをシリアル化してテストしました。Jackson は @JsonFormat を尊重しているように見えますが、@DateTimeFormat は完全に無視しています。

@DateTimeFormat を尊重するように spring/jackson を取得するために欠落している追加の構成は何ですか? @JsonFormat を使用するだけで発生する可能性のある問題、注意すべき 2 つの注釈の間に重要な違いはありますか?

4

1 に答える 1

2

@JsonFormatジャクソンの注釈です。@DateTimeFormatSpring アノテーションです。

@JsonFormatLocalDateTimeJSON へのシリアル化中のフォーマットを制御します。

@DateTimeFormatJackson は、JSP ビューでレンダリングされるときに Spring で Bean のフォーマットを制御するために使用されるSpring の について知りません。

Javadoc:

http://docs.spring.io/spring-framework/docs/4.2.3.RELEASE/javadoc-api/org/springframework/format/annotation/DateTimeFormat.html

http://static.javadoc.io/com.fasterxml.jackson.core/jackson-annotations/2.7.5/com/fasterxml/jackson/annotation/JsonFormat.html

于 2016-06-17T02:19:20.213 に答える