首页 文章

如何将Spring日期转换为LocalDate以获得Spring @RequestParam?

提问于
浏览
0

我有 @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] .wsmsDefaultHandlerExceptionResolver:无法绑定请求元素:org.springframework.web.method.annotation.MethodArgumentTypeMismatchException:转换失败类型'java.lang.String'的值为必需类型'java.time.LocalDate';嵌套异常是org.springframework.core.convert.ConversionFailedException:无法从类型[java.lang.String]转换为类型[@ org.springframework.web.bind.annotation.RequestParam @ org.springframework.format.annotation.DateTimeFormat java.time.LocalDate] for value'星期四2017年11月30日23:20:02 GMT-0600(中央标准时间)';嵌套异常是java.lang.IllegalArgumentException:解析尝试失败的值[Thu Nov 30 2017 23:20:02 GMT-0600(Central Standard Time)]

如何将日期的String表示(忽略时间组件)转换为LocalDate?

1 回答

  • 0

    尝试使用 pattern 属性:

    @DateTimeFormat(pattern="E MMM dd yyyy HH:mm:ss 'GMT'Z")
    

相关问题