【发布时间】:2018-08-30 09:14:33
【问题描述】:
我的控制器在请求映射中的可选参数有问题,请查看下面的控制器:
@GetMapping(produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<List<Books>> getBooks() {
return ResponseEntity.ok().body(booksService.getBooks());
}
@GetMapping(
produces = MediaType.APPLICATION_JSON_VALUE,
params = {"from", "to"}
)
public ResponseEntity<List<Books>>getBooksByFromToDate(
@RequestParam(value = "from", required = false) String fromDate,
@RequestParam(value = "to", required = false) String toDate)
{
return ResponseEntity.ok().body(bookService.getBooksByFromToDate(fromDate, toDate));
}
现在,当我发送如下请求时:
/getBooks?from=123&to=123
没关系,请求转到“getBooksByFromToDate”方法 但是当我使用发送类似的东西时:
/getBooks?from=123
或
/getBooks?to=123
它转到“getAlerts”方法
是否可以在 @RequestMapping 中设置可选参数 = {"from", "to"} ?有什么提示吗?
【问题讨论】:
-
你好,我也在找相同的,如果你有解决方案,请更新
标签: java spring rest api spring-boot