如何在 Kotlin 中从 RestController 解析 LocalDateTime
How to parsing LocalDateTime from RestController in Kotlin
我正在尝试使用 kotlin 为我的应用程序实现一个控制器。但是当我发出get请求时解析日期时间格式时出现错误。
@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]"
其余 api 在没有 dateTime 参数的情况下工作正常。
这是因为您提供的值的格式与定义的 ISO
日期不兼容。
这是您的价值(可能只是您身边的 "typo")和工作价值
2017-08-21T00:00:000.00 (yours)
2017-08-21T00:00:00.000 (working)
确保您提供有效的秒和毫秒值。
我正在尝试使用 kotlin 为我的应用程序实现一个控制器。但是当我发出get请求时解析日期时间格式时出现错误。
@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]"
其余 api 在没有 dateTime 参数的情况下工作正常。
这是因为您提供的值的格式与定义的 ISO
日期不兼容。
这是您的价值(可能只是您身边的 "typo")和工作价值
2017-08-21T00:00:000.00 (yours)
2017-08-21T00:00:00.000 (working)
确保您提供有效的秒和毫秒值。