2017-08-21 64 views
-2

我試圖用kotlin爲我的應用程序實現一個控制器。但是,當我發出get請求時,解析日期時間格式時出現錯誤。如何解析Kotlin中RestController的LocalDateTime

@RestController 
@RequestMapping("/api/records") 
class RecordController(val recordService: RecordService) { 

    @GetMapping("details") 
    fun getRecordDetail(
    @RequestParam recordId: Int, 
    @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) dateTime: LocalDateTime): RecordDto { 
    return recordService.getRecordDetails(recordId, dateTime) 
    } 
} 

的錯誤是

Failed to convert value of type 'java.lang.String' to required type 'java.time.LocalDateTime'; nested exception is org.springframework.core.convert.ConversionFailedException: 
Failed to convert from type [java.lang.String] to type [@org.springframework.web.bind.annotation.RequestParam @org.springframework.format.annotation.DateTimeFormat java.time.LocalDateTime] for value '2017-08-21T00:00:000.00'; nested exception is java.lang.IllegalArgumentException: 
Parse attempt failed for value [2017-08-21T00:00:000.00]" 

的REST API工作正常,沒有日期時間參數。

回答

0

這是因爲您提供的值格式與定義的ISO -date不兼容。

這裏你的價值(也許只是一個從你身邊「錯字」)和工作值

2017-08-21T00:00:000.00 (yours) 
2017-08-21T00:00:00.000 (working) 

確保您提供有效的秒和毫秒值。

+0

對不起,我的錯。我沒有正確檢查。非常感謝 –

相關問題