我目前正在試圖建模JSON API的查詢參數結構,並將其應用到我的Spring Boot項目中。我將關注filters,排序,分頁和可能的字段限制。如何在Spring中爲JSON API建模自定義請求參數?
我想先下手過濾,所以我想我的REST端點能夠處理像
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
JSON-API風格的URL請求,我的計劃是捕獲所有JSON API特定的查詢參數頂級JsonApiParams
對象,如:
public class JsonApiParams {
private Filters filters;
private Sorting sorting;
private Paging paging;
// getters, setters
}
然後模擬出Filters
,Sorting
,Paging
爲好。這JsonApiParams
對象將隨後在我@RestController
端點像這樣被接受的請求PARAM:
@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
)中的那些要求?