Currently, I am trying to return a list of Objects with a Date that falls into a range that is given by parameters… However, when I run my rest api call, nothing is returned. I am not sure if I am doing this Spring JPA request correctly and decided to get some feedback whether or not something I did is incorrect.
Here’s some brief pseudocode:
Object Model
public class mResult{
/* other variables */
LocalDate runDate;
}
Repo
public interface mResultRepository extends CrudRepository<mResult,String> {
List<mResult> findAllByRunDateBetween(LocalDate startDate,LocalDate endDate);
}
Service
mResultRepository mRepository;
public List<mResult> getAllBetweenDates(LocalDate startDate, LocalDate endDate){
return mRepository.findAllByRunDateBetween(startDate,endDate);
}
My repo contains JSON objects for example:
{"runDate": "2019-02-05"}
{"runDate": "2019-02-06"}
{"runDate": "2019-02-10}
If my range is 2019-02-01 to 2019-02-09, then it should only return those in this range. However, when I run my API request, nothing is returned. Is my Spring JPA request correct? TIA
I had the same issue some time back I try/find to get the solution from the database level, but I didn’t find any appropriate way to solve at DB level, So I would prefer it solve it from Java side as a sample code given below.
List<LocalDate> runningDates = Stream.iterate(startDate, date -> date.plusDays(1))
.limit(ChronoUnit.DAYS.between(startDate, endDate.plusDays(1)))
.collect(Collectors.toList());
I believe that it helps you.
Answer:
I added an @DateTimeFormat annotation to my controller. Turns out the Spring JPA Request was correct, but it wasn’t taking in my “yyyy-MM-dd” format.