Search code examples
javarestmodel-view-controllergetlocaldate

LocalDate format in Get request JAVA


I'm writing a REST interface for my Java MVC app. Here is one function:

    @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);
    }

Now I'm testing this, and

/rest/admin/restaurant_representation?id=1004

this requst provides a correct result, but when I try to add date parameter

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

it showes this:

The request sent by the client was syntactically incorrect.

What LocalDate format is correct?


Solution

  • We need to use @DateTimeFormat(iso = DateTimeFormat.ISO.DATE)

    like this:

        @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);
    }