簡體   English   中英

Get請求JAVA中的LocalDate格式

[英]LocalDate format in Get request JAVA

我正在為我的Java MVC應用程序編寫REST接口。 這是一個功能:

    @GetMapping(value = "/restaurant_representation", produces = MediaType.APPLICATION_JSON_VALUE)
    public RestaurantRepresentation restaurantRepresent(
        @RequestParam(value = "date", required = false) LocalDate date,
        @RequestParam(value = "id") Integer id) {
            return date == null ?
                restaurantRepresentationCompiler.compileRestaurantRepresentation(id, LocalDate.now()) :
                restaurantRepresentationCompiler.compileRestaurantRepresentation(id, date);
    }

現在我正在測試這個,並且

/ REST /管理/ restaurant_representation?ID = 1004

這個請求提供了正確的結果,但是當我嘗試添加日期參數時

/ REST /管理/ restaurant_representation?日期2015-05-05 =&ID = 1004

它表明了這一點:

客戶端發送的請求在語法上是不正確的。

什么LocalDate格式是正確的?

我們需要使用@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)

像這樣:

    @GetMapping(value = "/restaurant_representation", produces = MediaType.APPLICATION_JSON_VALUE)
    public RestaurantRepresentation restaurantRepresent(
        @RequestParam(value = "date", required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date,
        @RequestParam(value = "id") Integer id) {
            return date == null ?
                restaurantRepresentationCompiler.compileRestaurantRepresentation(id, LocalDate.now()) :
                restaurantRepresentationCompiler.compileRestaurantRepresentation(id, date);
}

您需要使用@DateTimeFormat並指定您接受的日期格式。

@RequestParam(required = false, value = "date") @DateTimeFormat(pattern="yyyy-MM-dd") LocalDate fromDate

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM