javascript - 如何将 Javascript 日期转换为 Spring @RequestParam 的 LocalDate?

标签 javascript java spring spring-mvc

我有一个 @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/

相关文章:

java - Java Swing 中 HMI 的分层

java - 如果请求参数超过 15 个,POST 或 GET 应该使用什么?

java - 考虑 Spring 中的接口(interface)代码?

javascript - 选择后下拉菜单的灯箱效果

javascript - 整个队列的 SWFUpload 进度条

javascript - 如何在 javascript 中获取页面的完整内容作为字符串?

JAVA移动球

java - 如何在输入流中写入

java - 是否可以将强类型模型传递给 View /jsp 页面?

javascript - 放置在 Bootstrap 轮播中的 D3.js 图表