My question is I need to implement rest api to search Students bases on its attributes below are my urls.
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/my system/vo1/students/search? responseType =summary
responseType can be summary,hierarchy or can more types will be added in feature.
So i have single URI "/my system/vo1/students/search?" with different parameters and user will send max two optional parameters and min is one is mandatory that is responseType
in spring we can map single uri with different request parameters to different method as shown below
@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
}
and so on .. we can write 9 different methods for this .
one more way is to do it create only one method.
@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
}
In second approach we can write separate validator to validate the input request.
I have below Questions
You should definitely go with one RequestMapping in this scenario (the 2nd option). What if you later want to combine query params? Combinatorial Explosion!
Make sure you understand the JPA Criteria API, or check out Jooq and QueryDSL. That will make life easier for you.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments