使用 QuerydslPredicateExecutor 使用 Spring 数据时更改请求 url 中的日期时间字符串格式

Change datetime string format in request url when using Spring data rest with QuerydslPredicateExecutor

在我的域中 class 我有一个字段:

public class Reservation {
    private LocalDateTime created = LocalDateTime.now();

在我的存储库中,我只想查找具有特定日期的预订(时间无关紧要):

public interface ReservationRepository extends Repository<Reservation, Long>, QuerydslPredicateExecutor<Reservation>, QuerydslBinderCustomizer<QReservation> {

        bindings.bind(root.created).first((path, value) -> path.between(value.withMinute(0).withHour(0), value.withMinute(0).withHour(0).plusDays(1).minusSeconds(1)));
    }
}

现在可以使用 url:

/reservations?created=01/20/16 00:00 AM"

但我想使用这种数据时间格式:

2016-01-20T00:00

据我了解 Spring 引导使用 RepositoryRestMvcConfiguration.class 进行自动配置的问题。默认情况下 TemporalAccessorParser.class 使用一些默认的 DateTimeFormatter。我想将其更改为

DateTimeFormatter ISO_LOCAL_DATE_TIME

如果只有 @DateTimeFormat 注释没有帮助,请尝试向项目添加自定义 Converter:

public class CustomStringToLocalDateTime implements Converter<String, LocalDateTime> {

    @Override
    public LocalDateTime convert(String source) {
        return LocalDateTime.parse(source);
    }
}

@Configuration
public class RepoRestConfig extends RepositoryRestConfigurerAdapter {
    @Override
    public void configureConversionService(ConfigurableConversionService conversionService) {
        conversionService.addConverter(String.class, LocalDateTime.class, new CustomStringToLocalDateTime());
        super.configureConversionService(conversionService);
    }
}

这种方法在我的项目中有效(除了我必须将日期的字符串表示形式 ('yyyy-MM-dd') 转换为 Instant (yyyy-MM-ddThh:mm:ssZ))。