3

我正在为我的 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/admin/restaurant_representation?id=1004

此请求提供了正确的结果,但是当我尝试添加日期参数时

/rest/admin/restaurant_representation?date=2015-05-05&id=1004

它显示了这一点:

客户端发送的请求在语法上不正确。

什么 LocalDate 格式是正确的?

4

2 回答 2

5

我们需要使用@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);
}
于 2017-01-07T10:45:47.023 回答
4

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

@RequestParam(required = false, value = "date") @DateTimeFormat(pattern="yyyy-MM-dd") LocalDate fromDate
于 2017-01-07T10:47:58.077 回答