我有一个 @RestController
方法如下:
@GetMapping("/getInvoices")
public List<InvoiceDto> getInvoices(@RequestParam(name="date") @DateTimeFormat LocalDate date) {
// do stuff and return a list of InvoiceDtos
}
但是当 JavaScript 客户端发送诸如 http://localhost:8080/getInvoices?date=Thu+Nov+30+2017+00%3A00%3A00+GMT-0600+(Central+Standard+Time) 之类的请求时
服务器报告:
2017-12-20 16:20:03.123 WARN 10324 --- [nio-9003-exec-7] .w.s.m.s.DefaultHandlerExceptionResolver : Failed to bind request element: org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'java.time.LocalDate'; 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.LocalDate] for value 'Thu Nov 30 2017 23:20:02 GMT-0600 (Central Standard Time)'; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [Thu Nov 30 2017 23:20:02 GMT-0600 (Central Standard Time)]
如何将日期的字符串表示形式(忽略时间部分)转换为 LocalDate?
最佳答案
尝试使用 pattern
属性:
@DateTimeFormat(pattern="E MMM dd yyyy HH:mm:ss 'GMT'Z")
关于javascript - 如何将 Javascript 日期转换为 Spring @RequestParam 的 LocalDate?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47915291/