通过 LocalDateFilter spring 休息控制器时收到错误请求
Getting bad request while passing LocalDateFilter spring rest controller
/api/test?page=-1&size=50&nextDateOfScreening.greaterThan=2020-04-03&sort=id,asc
这是我的传递 url 并且在控制器中收到它,
@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());
}
和ExampleCriteria class as,
public class ExampleCriteria implements Serializable, Criteria {
private LocalDateFilter nextDateOfScreening; //jhipster LocalDateFilter
}
我收到了错误的日期过滤器请求,
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 有错误。谁能帮我解决这个问题?
首先 greaterThanEquals
在 LocalDateFilter
中不存在。实际上是 greaterOrEqualThan
所以
nextDateOfScreening.greaterOrEqualThan
.
这是 "works" 的原因,因为 spring 没有找到 LocalDateFilter
setter,所以创建一个 "empty" LocalDateFilter
.
对于 lessThan
,已找到 setter,但未配置从 String
到 LocalDate
的转换。为了解决这个问题,您必须声明一个自定义转换器 ->
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"));
}
}
/api/test?page=-1&size=50&nextDateOfScreening.greaterThan=2020-04-03&sort=id,asc
这是我的传递 url 并且在控制器中收到它,
@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());
}
和ExampleCriteria class as,
public class ExampleCriteria implements Serializable, Criteria {
private LocalDateFilter nextDateOfScreening; //jhipster LocalDateFilter
}
我收到了错误的日期过滤器请求,
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 有错误。谁能帮我解决这个问题?
首先 greaterThanEquals
在 LocalDateFilter
中不存在。实际上是 greaterOrEqualThan
所以
nextDateOfScreening.greaterOrEqualThan
.
这是 "works" 的原因,因为 spring 没有找到 LocalDateFilter
setter,所以创建一个 "empty" LocalDateFilter
.
对于 lessThan
,已找到 setter,但未配置从 String
到 LocalDate
的转换。为了解决这个问题,您必须声明一个自定义转换器 ->
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"));
}
}