【问题标题】:How to convert Javascript date to LocalDate for a Spring @RequestParam?如何将 Javascript 日期转换为 Spring @RequestParam 的 LocalDate?
【发布时间】:2018-06-03 13:26:32
【问题描述】:

我有一个@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 : 绑定请求失败 元素: 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] 值'Thu Nov 30 2017 23:20:02 GMT-0600 (中部标准时间)';嵌套异常是 java.lang.IllegalArgumentException:值的解析尝试失败 [2017 年 11 月 30 日星期四 23:20:02 GMT-0600(中部标准时间)]

如何将日期的字符串表示形式(忽略时间部分)转换为 LocalDate?

【问题讨论】:

    标签: javascript java spring spring-mvc


    【解决方案1】:

    尝试使用pattern 属性:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-25
      • 1970-01-01
      • 1970-01-01
      • 2017-04-15
      • 2020-03-19
      • 2019-06-14
      • 2018-03-28
      • 2016-02-23
      相关资源
      最近更新 更多