/api/test?page=-1&size=50&nextDateOfScreening.greaterThan=2020-04-03&sort=id,asc
this is my passing url and in controller am receiving it as,
@GetMapping("/test")
public ResponseEntity<List<ExampleDTO>> getAllTIBenScrDetails(ExampleCriteria criteria, Pageable pageable) {
Page<ExampleDTO> page = tIBenScrDetailsQueryService.findByCriteria(criteria, pageable);
HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(ServletUriComponentsBuilder.fromCurrentRequest(), page);
return ResponseEntity.ok().headers(headers).body(page.getContent());
}
and ExampleCriteria class as,
public class ExampleCriteria implements Serializable, Criteria {
private LocalDateFilter nextDateOfScreening; //jhipster LocalDateFilter
}
and Iam getting bad request for the date filter as,
Field error in object 'ExampleCriteria' on field 'nextDateOfScreening.greaterThan': rejected value [2020-04-03]; codes [typeMismatch.ExampleCriteria.nextDateOfScreening.greaterThan,typeMismatch.nextDateOfScreening.greaterThan,typeMismatch.greaterThan,typeMismatch.java.time.LocalDate,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [ExampleCriteria.nextDateOfScreening.greaterThan,nextDateOfScreening.greaterThan]; arguments []; default message [nextDateOfScreening.greaterThan]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.time.LocalDate' for property 'nextDateOfScreening.greaterThan'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.time.LocalDate] for value '2020-04-03'; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [2020-04-03]]
For greaterThanEquals and lessThanEquals bad request error is not getting but for greaterThan and lessThan error is there. Can anyone help me to solve this?
First of all greaterThanEquals
doesn't exist in LocalDateFilter
. It is actually greaterOrEqualThan
so
nextDateOfScreening.greaterOrEqualThan
.
It is the reason it "works" because spring doesn't find the LocalDateFilter
setter and so create an "empty" LocalDateFilter
.
For lessThan
the setter is found but conversion of String
to LocalDate
is not configured. And to solve that you have to declare a custom converter ->
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
@Component
public class LocalDateConverter implements Converter<String, LocalDate> {
@Override
public LocalDate convert(final String s) {
return LocalDate.parse(s, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
}
}