如何为Spring数据Rest存储库和Querydsl定义请求日期格式
简介 假设在Spring boot应用程序中,有一个person repository接口,它扩展了如何为Spring数据Rest存储库和Querydsl定义请求日期格式,spring,spring-boot,spring-data-rest,querydsl,java-time,Spring,Spring Boot,Spring Data Rest,Querydsl,Java Time,简介 假设在Spring boot应用程序中,有一个person repository接口,它扩展了JpaRepository、querydsldpredicateexecutor和QuerydslBinderCustomizer,如下所示: @RepositoryRestResource(path = "persons") public interface PersonRepository extends JpaRepository<Person, Long&
JpaRepository
、querydsldpredicateexecutor
和QuerydslBinderCustomizer
,如下所示:
@RepositoryRestResource(path = "persons")
public interface PersonRepository
extends JpaRepository<Person, Long>, QuerydslPredicateExecutor<Person>,
QuerydslBinderCustomizer<QPerson> {
@Override
default void customize(QuerydslBindings bindings, QPerson root) {
bindings.bind(String.class)
.first((StringPath path, String value) -> (path.eq(value)));
bindings.bind(LocalDate.class)
.first((DatePath<LocalDate> path, LocalDate value) -> (path.eq(value)));
}
}
此请求已成功处理
问题
但是如何(集中)定义查询所有生日相等的人的日期格式?
我想这样查询存储库(格式为yyyy-MM-dd):
运行应用程序的系统区域设置为en_US,因此日期格式类似于MM/dd/yy。(使用此日期格式进行查询,请求也会成功处理)
第一个(未成功的)想法/尝试
要使用预期的格式,我的第一个想法是在spring boot的应用程序属性中定义日期格式:
spring.mvc.format.date: yyyy-MM-dd
但这种方法导致出现DateTimeParseException
表示无法解析格式为yyyy-MM-dd
的日期:
...
Caused by: java.lang.IllegalArgumentException: Parse attempt failed for value [2020-01-01]
at org.springframework.format.support.FormattingConversionService$ParserConverter.convert(FormattingConversionService.java:223) ~[spring-context-5.2.10.RELEASE.jar:5.2.10.RELEASE]
at org.springframework.core.convert.support.ConversionUtils.invokeConverter(ConversionUtils.java:41) ~[spring-core-5.2.10.RELEASE.jar:5.2.10.RELEASE]
... 56 common frames omitted
Caused by: java.time.format.DateTimeParseException: Text '2020-01-01' could not be parsed at index 4
at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2046) ~[na:na]
at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1948) ~[na:na]
at java.base/java.time.LocalDate.parse(LocalDate.java:428) ~[na:na]
at org.springframework.format.datetime.standard.TemporalAccessorParser.parse(TemporalAccessorParser.java:69) ~[spring-context-5.2.10.RELEASE.jar:5.2.10.RELEASE]
at org.springframework.format.datetime.standard.TemporalAccessorParser.parse(TemporalAccessorParser.java:46) ~[spring-context-5.2.10.RELEASE.jar:5.2.10.RELEASE]
at org.springframework.format.support.FormattingConversionService$ParserConverter.convert(FormattingConversionService.java:217) ~[spring-context-5.2.10.RELEASE.jar:5.2.10.RELEASE]
(“以LocalDate
为参数手动创建的常规”存储库资源使用所需的日期格式,因此成功处理../persons/search/findByDateOfBirth=1987-12-15
等请求)
作为一个完整的示例,我创建了一个包含一些测试的可执行文件。提供了一个带有
RepositoryRestConfigurer
的自定义转换器,解决了我的问题,因此可以指定日期格式(Jsr310Converters.StringToLocalDateConverter.INSTANCE
使用DateTimeFormatter.ISO\u date
):
一般而言:
@Component
public class LocalDateConfiguration implements RepositoryRestConfigurer {
@Override
public void configureConversionService(ConfigurableConversionService conversionService) {
conversionService.addConverter(new Converter<String, LocalDate>() {
@Override
public LocalDate convert(String source) {
// Use any format
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
return LocalDate.parse(source, formatter);
}
});
}
}
@组件
公共类LocalDateConfiguration实现RepositoryRestConfigurer{
@凌驾
public void configureConversionService(ConfigurableConversionService conversionService){
conversionService.addConverter(新转换器(){
@凌驾
公共LocalDate转换(字符串源){
//使用任何格式
DateTimeFormatter formatter=模式的DateTimeFormatter.of(“yyyy-MM-dd”);
返回LocalDate.parse(源,格式化程序);
}
});
}
}
提供带有RepositoryRestConfigurer
的自定义转换器解决了我的问题,因此可以指定日期格式(Jsr310Converters.StringToLocalDateConverter.INSTANCE
使用DateTimeFormatter.ISO_date
):
一般而言:
@Component
public class LocalDateConfiguration implements RepositoryRestConfigurer {
@Override
public void configureConversionService(ConfigurableConversionService conversionService) {
conversionService.addConverter(new Converter<String, LocalDate>() {
@Override
public LocalDate convert(String source) {
// Use any format
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
return LocalDate.parse(source, formatter);
}
});
}
}
@组件
公共类LocalDateConfiguration实现RepositoryRestConfigurer{
@凌驾
public void configureConversionService(ConfigurableConversionService conversionService){
conversionService.addConverter(新转换器(){
@凌驾
公共LocalDate转换(字符串源){
//使用任何格式
DateTimeFormatter formatter=模式的DateTimeFormatter.of(“yyyy-MM-dd”);
返回LocalDate.parse(源,格式化程序);
}
});
}
}
@Component
public class LocalDateConfiguration implements RepositoryRestConfigurer {
@Override
public void configureConversionService(ConfigurableConversionService conversionService) {
conversionService.addConverter(StringToLocalDateConverter.INSTANCE);
}
}
@Component
public class LocalDateConfiguration implements RepositoryRestConfigurer {
@Override
public void configureConversionService(ConfigurableConversionService conversionService) {
conversionService.addConverter(new Converter<String, LocalDate>() {
@Override
public LocalDate convert(String source) {
// Use any format
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
return LocalDate.parse(source, formatter);
}
});
}
}