2017-04-11 16 views
2

から型java.time.LocalDateTimeの値をデシリアライズすることはできませんは、私が設定以下のいる文字列

ext { 
     springBootVersion = '1.5.2.RELEASE' 
    } 
.... 
dependencies { 
    compile('org.springframework.boot:spring-boot-starter-websocket') 
    compile("org.springframework:spring-messaging") 
    compile('org.springframework.boot:spring-boot-starter-actuator') 
    compile('org.springframework.boot:spring-boot-starter-thymeleaf') 
    compile('org.springframework.boot:spring-boot-starter-validation') 
    compile('org.springframework.boot:spring-boot-starter-web') 
    compile group: 'net.jcip', name: 'jcip-annotations', version: '1.0' 
    compile ("com.fasterxml.jackson.datatype:jackson-datatype-jsr310") 
    testCompile('org.springframework.boot:spring-boot-starter-test') 
} 

を私は、次のコントローラを追加しました:

@PostMapping("/validation_test") 
    public String testValidation(@Valid @RequestBody ClientInputMessage clientInputMessage, BindingResult result) { 
     logger.info(Arrays.toString(result.getAllErrors().toArray())); 
     return "main"; 
    } 


public class ClientInputMessage { 
    @NotEmpty 
    private String num1; 
    @NotEmpty 
    private String num2; 
    @Past 
    private LocalDateTime date; 

このようなjsonを渡すと:

出力は次のよう枚の
{ 
     "num1":"324", 
     "num2":123, 
     "date":"2014-01-01" 
    } 

アプリケーション版画:

Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Can not deserialize value of type java.time.LocalDateTime from String "2014-01-01": Text '2014-01-01' could not be parsed at index 10 
at [Source: [email protected]; line: 4, column: 8] (through reference chain: model.ClientInputMessage["date"]); nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Can not deserialize value of type java.time.LocalDateTime from String "2014-01-01": Text '2014-01-01' could not be parsed at index 10 
at [Source: [email protected]; line: 4, column: 8] (through reference chain: model.ClientInputMessage["date"]) 
+4

「2014-01-01T00:00:00」と入力してください。 javaのLocalDateTimeは有効な日付文字列として "2014-01-01"を受け入れません。 –

+1

@Jure Kolenko OOPS、それは動作します – gstackoverflow

+1

"2014-01-01"のような日付のみの値は、 'LocalDateTime'クラスではなく' LocalDate'クラスを使います。また、問題が解決した場合、あなたまたはコレンコは、この質問を閉じるために回答を投稿する必要があります。 –

答えて

4

オリジナルの答え:Javaで

のLocalDateTimeが有効な日付文字列として "2014年1月1日" を受け付けません。

いくつかの追加情報:

あなたが実際にあなたの日付を入力する気にしない場合は(LOCALDATE、OffsetDate、ZonedDate、...)である、あなたはそれTemporalAccessor作ることができ、その後、日付を解析するためにDateTimeFormatter::parseBestを使用。

P.S.
LocalDateTimeには「2014-01-01T00:00:00」という文字列が有効です

+3

を受け付けています。参考:java.namのクラスドキュメントでは、一般的に 'TemporalAccessor'のようなより一般的なインターフェースの使用を避けるようお勧めします。そのようなインタフェースはフレームワーク内での使用のみを意図しています。見積もり:*このインタフェースは、アプリケーションコードで広く使用されるべきではないフレームワークレベルのインタフェースです。その代わりに、アプリケーションは具体的な型のインスタンスを作成し渡す必要があります... * –

関連する問題