【发布时间】:2020-01-23 13:19:50
【问题描述】:
我的控制器方法
@GetMapping("/fetch/{one_date}/{two_date}")
public List<CourierInfo> getData_between(@PathVariable(value = "one_date") @DateTimeFormat(pattern = "dd-MM-yyyy HH:mm:ss") Date fromDate, @PathVariable(value = "two_date") @DateTimeFormat(pattern = "dd-MM-yyyy") Date toDate) throws Exception{
System.out.println(fromDate);
System.out.println(toDate);
String date1 = "20-01-2020";
String date2 = "24-01-2020";
Date d1 = new SimpleDateFormat("dd-MM-yyyy").parse(date1);
Date d2 = new SimpleDateFormat("dd-MM-yyyy").parse(date2);
return bookRepository.getData_between(d1, d2);
}
我正在传递日期的网址
http://localhost:8080/book_api/fetch/20-01-2020/24-01-2020
我收到了这个错误
无法将“java.lang.String”类型的值转换为所需的“java.util.Date”类型;嵌套异常是 org.springframework.core.convert.ConversionFailedException: 无法从类型 [java.lang.String] 转换为类型 [@org.springframework.web.bind.annotation.PathVariable @org.springframework.format.annotation.DateTimeFormat java.util.Date] 的值'20-01-2020';嵌套异常是 java.lang.IllegalArgumentException:值 [20-01-2020] 的解析尝试失败
我该如何解决这个错误。我尝试了不同的方法。
【问题讨论】:
-
@DateTimeFormat(pattern = "dd-MM-yyyy HH:mm:ss")然后你传递了一个没有时间的日期 -
您传递了格式为
dd-MM-yyyy的日期,但您试图将其解析为dd-MM-yyy HH:mm:ss
标签: java rest spring-boot