在两个日期之间搜索数据

在两个日期之间搜索数据

我想在两个日期之间搜索数据,这是我的存储库

@Query("select mvt from Mouvements mvt where mvt.dateCompte between :x and :y")
public Page<Mouvements> getMouvementsByDate(@Param("x")LocalDate dateBefore, @Param("y")LocalDate dateAfter, Pageable pageable);


这是我的控制器

@RequestMapping(value="/movementsByDate", method = RequestMethod.GET)
public Page<Mouvements> movementsByDate(
        @RequestParam(name= "dateBefore", defaultValue="")LocalDate dateBefore,
        @RequestParam(name= "dateAfter", defaultValue="")LocalDate dateAfter,
        @RequestParam(name= "page", defaultValue="0")int page,
        @RequestParam(name= "size", defaultValue="5")int size){
    return mouvementsRepository.getMouvementsByDate(dateBefore, dateAfter, new PageRequest(page, size));
}


现在我的问题是当我使用以下URL测试此Web服务时:

http://localhost:8080/api/movement/movementsByDate?dateBefore=27/11/2015&dateAfter=03/01/2019&page=0&size=9


我有这个错误

Failed to convert value of type 'java.lang.String' to required type 'java.time.LocalDate'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@org.springframework.web.bind.annotation.RequestParam java.time.LocalDate] for value '27/11/2015'; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [27/11/2015]",

最佳答案

最好以字符串形式接收参数,然后在控制器中将字符串转换为LocalDate。

@RequestMapping(value="/movementsByDate", method = RequestMethod.GET)
public Page<Mouvements> movementsByDate(
        @RequestParam(name= "dateBefore", defaultValue="")String dateBeforeString,
        @RequestParam(name= "dateAfter", defaultValue="")String dateAfterString,
        @RequestParam(name= "page", defaultValue="0")int page,
        @RequestParam(name= "size", defaultValue="5")int size){

        LocalDate dateBefore = LocalDate.parse(dateBeforeString, DateTimeFormatter.ofPattern("dd/MM/yyyy"));
        LocalDate dateAfter = LocalDate.parse(dateAfterString, DateTimeFormatter.ofPattern("dd/MM/yyyy"));

    return mouvementsRepository.getMouvementsByDate(dateBefore, dateAfter, new PageRequest(page, size));
}

07-24 20:40