Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Postgresql 如何为jpa/hibernate查询提供LocalDateTime?_Postgresql_Hibernate_Spring Boot_Jpa_Spring Data Rest - Fatal编程技术网

Postgresql 如何为jpa/hibernate查询提供LocalDateTime?

Postgresql 如何为jpa/hibernate查询提供LocalDateTime?,postgresql,hibernate,spring-boot,jpa,spring-data-rest,Postgresql,Hibernate,Spring Boot,Jpa,Spring Data Rest,我正在@RepositoryRestResource中生成一个查询 其中查询如下所示: @Query("Select DISTINCT comp from InsuranceCompany comp " + "LEFT JOIN comp.orders ord " + "wHERE ord.invoiced = false " + "and (:date is null or :date >= ord.completi

我正在@RepositoryRestResource中生成一个查询

其中查询如下所示:

@Query("Select DISTINCT comp from InsuranceCompany comp " +
            "LEFT JOIN comp.orders ord " +
            "wHERE ord.invoiced = false " +
            "and (:date is null or :date >= ord.completionTime)"
    )
public Page<InsuranceCompany> method(LocalDateTime date, Pageable pageable);
当我用以下命令调用端点时:

GET /method?date=2020-02-14T15:50:24

Spring默认情况下无法将REST参数转换为LocalDateTime。您需要在带有@DateTimeFormat注释的参数级别或全局使用DateTimeFormatterRegistrator提供有关日期格式的信息


本文解释了两种选择:

@DateTimeFormat
标记它,让Spring正确转换它:

public Page<InsuranceCompany> method(@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime date, 
                                     Pageable pageable);
publicpage方法(@DateTimeFormat(iso=DateTimeFormat.iso.DATE\u-TIME)LocalDateTime日期,
可寻呼(可寻呼);
选项1:为所有Spring Boot App REST端点全局设置日期/时间格式 您可以全局配置spring,以便为REST端点使用特定的日期/日期时间格式。建议您使用默认Jackson来处理JSON映射,您可以创建一个配置类,如下所示,在其中设置格式:

@Configuration
public class DateTimeSerializationConfiguration implements Jackson2ObjectMapperBuilderCustomizer {

    private static final DateTimeFormatter DATE_FORMATTER = ISO_LOCAL_DATE;
    private static final DateTimeFormatter DATE_TIME_FORMATTER = ISO_DATE_TIME;
    private static final DateTimeFormatter TIME_FORMATTER = ofPattern("HH:mm");

    @Bean
    public Formatter<LocalDate> localDateFormatter() {
        return new Formatter<LocalDate>() {
            @Override
            public LocalDate parse(String text, Locale locale) {
                return LocalDate.parse(text, DATE_FORMATTER);
            }

            @Override
            public String print(LocalDate object, Locale locale) {
                return DATE_FORMATTER.format(object);
            }
        };
    }

    @Bean
    public Formatter<LocalDateTime> localDateTimeFormatter() {
        return new Formatter<LocalDateTime>() {
            @Override
            public LocalDateTime parse(String text, Locale locale) {
                return LocalDateTime.parse(text, DATE_TIME_FORMATTER);
            }

            @Override
            public String print(LocalDateTime object, Locale locale) {
                return DATE_TIME_FORMATTER.format(object);
            }
        };
    }

    @Bean
    public Formatter<LocalTime> localTimeFormatter() {
        return new Formatter<LocalTime>() {
            @Override
            public LocalTime parse(String text, Locale locale) {
                return LocalTime.parse(text, TIME_FORMATTER);
            }

            @Override
            public String print(LocalTime object, Locale locale) {
                return TIME_FORMATTER.format(object);
            }
        };
    }

    @Override
    public void customize(Jackson2ObjectMapperBuilder jacksonObjectMapperBuilder) {
        jacksonObjectMapperBuilder.serializers(
            new LocalDateSerializer(DATE_FORMATTER),
            new LocalDateTimeSerializer(DATE_TIME_FORMATTER),
            new LocalTimeSerializer(TIME_FORMATTER));
        jacksonObjectMapperBuilder.deserializers(
            new LocalDateDeserializer(DATE_FORMATTER),
            new LocalDateTimeDeserializer(DATE_TIME_FORMATTER),
            new LocalTimeDeserializer(TIME_FORMATTER));
    }

}


选项2:分别为每个REST端点设置日期/时间格式 如果希望单独设置每个端点的格式,则必须使用
@DateTimeFormat
注释请求参数,并指定预期的格式。下面的示例显示了如何实现这一点的不同示例:

@RestController
public class BookingController {

    private final YourService yourService;

    @Autowired
    public BookingController(YourService yourService) {
        this.yourService = yourService;
    }

    @GetMapping("/your/api/endpoint")
    public YourObject yourControllerMethod(@RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date, Pageable pageable) {
        return yourService.yourServiceMethod(date, pageable);
    }

    // Or: with LocalDateTime

    @GetMapping("/your/api/endpoint")
    public YourObject yourControllerMethod(@RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime dateTime, Pageable pageable) {
        return yourService.yourServiceMethod(dateTime, pageable);
    }

    // Or: with your custom pattern

    @GetMapping("/your/api/endpoint")
    public YourObject yourControllerMethod(@RequestParam @DateTimeFormat(pattern = "dd.MM.yyyy") LocalDate date, Pageable pageable) {
        return yourService.yourServiceMethod(date, pageable);
    }
}
@RestController
public class BookingController {

    private final YourService yourService;

    @Autowired
    public BookingController(YourService yourService) {
        this.yourService = yourService;
    }

    @GetMapping("/your/api/endpoint")
    public YourObject yourControllerMethod(@RequestParam LocalDate date, Pageable pageable) {
        return yourService.yourServiceMethod(date, pageable);
    }

    // Or: with LocalDateTime

    @GetMapping("/your/api/endpoint")
    public YourObject yourControllerMethod(@RequestParam LocalDateTime dateTime, Pageable pageable) {
        return yourService.yourServiceMethod(dateTime, pageable);
    }
}
@RestController
public class BookingController {

    private final YourService yourService;

    @Autowired
    public BookingController(YourService yourService) {
        this.yourService = yourService;
    }

    @GetMapping("/your/api/endpoint")
    public YourObject yourControllerMethod(@RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date, Pageable pageable) {
        return yourService.yourServiceMethod(date, pageable);
    }

    // Or: with LocalDateTime

    @GetMapping("/your/api/endpoint")
    public YourObject yourControllerMethod(@RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime dateTime, Pageable pageable) {
        return yourService.yourServiceMethod(dateTime, pageable);
    }

    // Or: with your custom pattern

    @GetMapping("/your/api/endpoint")
    public YourObject yourControllerMethod(@RequestParam @DateTimeFormat(pattern = "dd.MM.yyyy") LocalDate date, Pageable pageable) {
        return yourService.yourServiceMethod(date, pageable);
    }
}