【发布时间】:2015-02-21 02:52:27
【问题描述】:
我的问题是我需要实现 rest api 来根据它的属性搜索学生 以下是我的网址。
http/my system/vo1/students/search?firstname=ABC&responseType=summary http/my system/vo1/students/search?age=ABC&responseType=summary http/my system/vo1/students/search?id=ABC& responseType =summary http/my system/vo1/students/search?mobile=ABC& responseType =summary http/my system/vo1/students/search?lastname=ABC& responseType =summary http/my system/vo1/students/search?education=ABC& responseType=summary http/my system/vo1/students/search?working=ABC& responseType =summary http/我的系统/vo1/students/search? responseType =总结responseType可以是summary,hierarchy,也可以在feature中添加更多类型。
所以我只有一个 URI“/my system/vo1/students/search?”有不同的参数 并且用户将发送 max 两个可选参数,min 是一个强制参数,即 responseType
在 spring 中,我们可以将具有不同请求参数的单个 uri 映射到不同的方法,如下所示
@RequestMapping(value = “my system/vo1/students/search", method = RequestMethod.GET)
@ResponseBody
public Students searchStudentByName(
@RequestParam(value = “name",required = true) String name,
@RequestParam(value = “ responseType", required = true) String responseType)
{
//do student fetching works
}
@RequestMapping(value = “my system/vo1/students/search", method = RequestMethod.GET)
@ResponseBody
public Students searchStudentByAge(
@RequestParam(value = “age",required = true) int age,
@RequestParam(value = “ responseType", required = true) String responseType)
{
//do student fetching works
}
等等.. 我们可以为此编写 9 种不同的方法。
另一种方法是只创建一种方法。
@RequestMapping(value = “my system/vo1/students/search", method = RequestMethod.GET)
@ResponseBody
public Students searchStudentByAge(
@RequestParam(value = “firstname",required = true) String age,
@RequestParam(value = “age",required = false) int age,
@RequestParam(value = “lastname",required = false) String lastname,
@RequestParam(value = “working",required = false) String working,
//add more for each field that will be present in url...........
@RequestParam(value = “ responseType", required = true) String responseType)
{
//do student fetching works
}
在第二种方法中,我们可以编写单独的验证器来验证输入请求。
我有以下问题
- 哪种方法最好(考虑最佳设计原则和实践)
- 为什么这是最好的方法
- 针对此类问题的最佳面向对象设计是什么
【问题讨论】:
标签: java spring spring-mvc