簡體   English   中英

如何在兩個日期之間獲取數據REST Spring

[英]How to get data between two dates 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);

我路過

http://localhost:8080/book_api/fetch/2020-01-20/2020-01-20

在這里,我試圖在兩個日期之間獲取數據。 我收到此錯誤

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"]

檢查這個主題: https : //stackoverflow.com/a/53188501/4135813

該問題與您的端點配置中缺少轉換器有關

首先停止使用java.util.Date並從 java-8 日期時間 API 開始使用LocalDate ,您可以使用DateTimeFormatter將輸入的日期字符串解析為LocalDate

@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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM