Get 请求中的 LocalDate 格式 JAVA
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/admin/restaurant_representation?id=1004
这个请求提供了正确的结果,但是当我尝试添加日期参数时
/rest/admin/restaurant_representation?date=2015-05-05&id=1004
显示的是:
The request sent by the client was syntactically incorrect.
哪种 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
我正在为我的 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
显示的是:
The request sent by the client was syntactically incorrect.
哪种 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