如何解析Kotlin中RestController的LocalDateTime
问题描述:
我试图用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工作正常,没有日期时间参数。
答
这是因为您提供的值格式与定义的ISO
-date不兼容。
这里你的价值(也许只是一个从你身边“错字”)和工作值
2017-08-21T00:00:000.00 (yours)
2017-08-21T00:00:00.000 (working)
确保您提供有效的秒和毫秒值。
对不起,我的错。我没有正确检查。非常感谢 –