Spring boot中Get請求中Date參數的傳遞

問題

想要在Get請求中,傳遞Date參數,以下:html

@GetMapping("/findByPeriodOrderByTimeDesc")
public ResponseEntity<List<Message>> getMessagesPeriodByTimeDesc(@RequestParam @DateTimeFormat Date start, @RequestParam @DateTimeFormat Date end) {
}

儘管,我這裏使用了DateTimeFormat的註解,然而,Spring依舊把Date參數看成String類型來處理。以前在文章《spring boot中jackson時間格式和東八區的設置》中,使用以下配置:spring

# JACKSON (JacksonProperties)
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone=GMT+8

DateTimeFormat有沒有相似到配置類?mvc

解決

application.properties

spring.mvc.date-format=yyyy-MM-dd HH:mm:ss

Java

@GetMapping("/findByPeriodOrderByTimeDesc")
public ResponseEntity<List<Message>> getMessagesPeriodByTimeDesc(@RequestParam Date start, @RequestParam Date end) {
}

調試

Postman

Get請求

效果

解決

參考

相關文章
相關標籤/搜索