【发布时间】:2018-05-26 11:50:23
【问题描述】:
在我的 Spring Boot 应用程序中,我有一个问题作为资源,具有以下字段。
[
{
"questionId":6,
"area":"TECHNICAL",
"title":"Find the index of first 1 in an infinite sorted array of 0s and 1s",
"description":"Given an infinite sorted array consisting 0s and 1s. The problem is to find the index of first 1 in that array. As the array is infinite, therefore it is guaranteed that number 1 will be present in the array.",
"state":"ACTIVE",
"difficultyLevel":"EASY",
"skills":[
{
"skillId":1,
"skillName":"ALGORITHM"
},
{
"skillId":2,
"skillName":"PROGRAMMING"
}
],
"proposedBy":"agrawalo",
"noOfTimesUsed":0,
"examples":null,
"probes":null,
"approvedBy":null,
"addedBy":null,
"dateCreated":"2018-05-16T19:29:11.113",
"dateLastUpdated":"2018-05-16T19:29:11.113"
},
{
...
},
...
]
我想编写控制器来过滤问题。
例如:
1./questions?area="技术"。返回区域为“技术”的问题的控制器。为此,我编写了以下方法。
@RestController
public class QuestionController {
@RequestMapping("/questions", method = GET)
String getFilteredQuestions(@RequestParam("area") String questionArea) {
}
}
2./questions?area="technical"&skill="programming"。现在,我想编写一个控制器,它返回区域为“技术”和技能为“编程”的问题。
实现此目的的一种方法是向“getFilteredQuestion”方法添加一个请求参数。
但是,我最终会编写一个非常混乱的代码(例如,如果我想添加更多过滤器)来检查哪些请求参数为空,哪些不是,并基于该应用过滤器。
@RestController
public class QuestionController {
@RequestMapping("/questions", method = GET)
String getFilteredQuestions(@RequestParam("area") String questionArea, @RequestParam("skill") String questionSkill) {
}
}
3./questions?skill="algorithm"&proposedby="agrawalo" ...
有什么简洁的方法可以编写这样的控制器吗?
【问题讨论】:
-
你可以使用库 rsql-parser stackoverflow.com/a/56539774
标签: java rest spring-boot spring-restcontroller