I'm trying to use QueryDSL Spring data web support to filter results of a REST requests based on the value of a Date passed through as a request parameter.
Currently I build the predicate myself from the date passed in as a String:
@GetRequest("/foos")
public List<Foo> getFoos(@RequestParam(name = "date", required = false)
@DateTimeFormat(pattern = "yyyy-MM-dd") Date date) {
Predicate predicate = QFoo.date.eq(date);
...
}
But I want to do something like:
@GetRequest("/foos")
public List<Foo> getFoos(@QueryDslPredicate(root = Foo.class) Predicate predicate) {
...
}
But obviously it fails to parse the Date since it doesn't have any information about the pattern of the Date string anymore. Is there any way to get this QueryDSL web support working with Date objects?
Try to add @DateTimeFormat
to the entity field:
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date date;