【发布时间】:2017-10-14 00:59:09
【问题描述】:
我目前正在尝试对JSON API 的查询参数结构进行建模,并将其应用于我的 Spring Boot 项目中。我将专注于filters、排序、分页,也许还有字段限制。
我想先从过滤开始,所以我希望我的 REST 端点能够处理 JSON-API 样式的 URL 请求,例如
GET /comments?filter[post]=1 HTTP/1.1
GET /comments?filter[post]=1,2 HTTP/1.1
GET /comments?filter[post]=1,2&filter[author]=12 HTTP/1.1
我的计划是在顶级 JsonApiParams 对象中捕获所有 JSON API 特定的查询参数,例如:
public class JsonApiParams {
private Filters filters;
private Sorting sorting;
private Paging paging;
// getters, setters
}
然后模拟出Filters、Sorting、Paging。这个JsonApiParams 对象将在我的@RestController 端点中作为请求参数被接受,如下所示:
@RequestMapping(value = {"/api/v1/{entity}"},
method = RequestMethod.GET,
produces = {"application/vnd.api+json"})
@ResponseBody
public JsonApiTopLevel jsonApiGetByEntity(@PathVariable String entity, JsonApiParams params) {
// convert params to DB query
}
那么,我应该如何为我的JsonApiParams 对象建模,以便能够处理上述请求(例如/comments?filter[post]=1,2&filter[author]=12)?
【问题讨论】: