如何在两个日期之间获取数据REST Spring [英] How to get data between two dates REST Spring
本文介绍了如何在两个日期之间获取数据REST Spring的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的控制器映射
@GetMapping("/fetch/{one_date}/{two_date}")
public List<CourierInfo> getData_between(@PathVariable(value = "one_date") @DateTimeFormat(pattern = "yyyyMMdd") LocalDateTime fromDate, @PathVariable(value = "two_date") @DateTimeFormat(pattern = "yyyyMMdd") LocalDateTime toDate) {
return bookRepository.getData_between(fromDate, toDate);
}
我的自定义查询
@Query(nativeQuery = true, value="select c.cons_no, c.pick_date, from CourierInfo c where c.pick_date between :startDate and :endDate")
List getData_between(@Param("startDate") LocalDateTime date, @Param("endDate") LocalDateTime date2);
List getData_between(@Param("startDate") LocalDateTime date, @Param("endDate") LocalDateTime date2);
我通过了
http://localhost:8080/book_api/fetch/2020-01-20/2020-01-20
在这里,我试图在两个日期之间获取数据.我收到此错误
Here I am trying to fetch data between two dates. I am getting this error
Failed to convert value of type 'java.lang.String' to required type 'java.time.LocalDateTime'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@org.springframework.web.bind.annotation.PathVariable @org.springframework.format.annotation.DateTimeFormat java.time.LocalDateTime] for value '"2020-01-20"'; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value ["2020-01-20"]
推荐答案
首先停止使用 java.util.Date
并从 java-8 日期开始使用 LocalDate
时间 API,您可以使用 DateTimeFormatter
First of all stop using java.util.Date
and start using LocalDate
from java-8 date time API, you can parse the input date string into LocalDate
using DateTimeFormatter
@GetMapping("/fetch/{one_date}/{two_date}")
public List<CourierInfo> getData_between(@PathVariable(value = "one_date") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate fromDate, @PathVariable(value = "two_date") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate toDate) {
return bookRepository.getData_between(fromDate, toDate);
}
在仓库中
@Query(nativeQuery = true, value="select c.cons_no, c.pick_date, from CourierInfo c where c.pick_date between :startDate and :endDate")
List<CourierInfo> getData_between(@Param("startDate") LocalDate date, @Param("endDate") LocalDate date2);
这篇关于如何在两个日期之间获取数据REST Spring的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文