RestFul API Parameters Implementation

In this section, we will be describing the RESTFUL API Parameters implementation like Query param and Path params which are most commonly used across the projects. Each method has its unique features and functionalities.

If you want to learn how to create a simple Rest application, please find from the below video.

 

Now, Let’s look at the 2 params with samples.

1. Request Params:

If you are a web developer, you must have learned about how to transfer data using the GET method in a most simple way.
Imagine that you’re creating some social service. Then you have to control the user list. Each user has their own page.
When you want to create this page for each user, you need to path some identified parameter to that page.
You can achieve that using something like this kind of get parameter:

https://www.youtube.com/watch?v=ejiSLiGwep4

From the above link, you can get ‘v’ variable in the backend. This is a Query String how it works.

2. Path Params Sample:

But, in another way, you can use Path Variable as one of the passing data methods. You can define like this:

http://www.facebook.com/CoolTechGuys
In this case, you can pass CoolTechGuys to the backend. You can use the path as a variable.

3. Depends on the framework, syntax will be changed :

The following query params and path params depend upon the below 2 Framework.

  1. Jersey (JAX-RS)
  2. Spring RESTFul
Framework                   Path segment               http query parameter
1. Jersey (JAX-RS)          @PathParam                 @QueryParam                       
2. Spring RESTFul           @PathVariable              @RequestParam
example                     http://xyz.ir/{segment}    http://xyz.ir/?param{param}
4. Query params Schenaio:

Please find the below list of conditions which can be developed in Query params,

/**
* Query Parameter : Condition 1
* Default = true
* http://localhost:9002/student/getId?id=1234&name=CTG
* @param id
* @param name
* @return
*/
@RequestMapping(value = "/getId", method = RequestMethod.GET)
@ResponseBody
public String getDetails(@RequestParam(value = "id") String id, @RequestParam String name) { 
return "Student Id: " + id + " Student Name: " + name;
}
/**
* Query Parameter : Condition 2
* 
* Request -> http://localhost:9002/student/getIdCheck
* Response 1 -> Student is mandatory
* Response 2 -> Student is 123
* @param id
* @param name
* @return
*/
@RequestMapping(value = "/getIdCheck", method = RequestMethod.GET)
@ResponseBody
public String getDetails(@RequestParam(value = "id", required = false) String id) { 
return "Student id " + (id != null ? "is mandatory" : id);
}
/**
* Query Parameter : Condition 3
* 
* Request -> http://localhost:9002/student/default
* Response -> Student is 12345
* @param id
* @param name
* @return
*/
@RequestMapping(value = "/default", method = RequestMethod.GET)
@ResponseBody
public String testDefault(@RequestParam(value = "id", defaultValue = "12345") String id) { 
return "Student id " + id;
}
/**
* Query Parameter : Condition 4
* 
* Request -> http://localhost:9002/student/list?id=1,2,3
* Response -> Student id's are [1,2,3]
* @param id
* @param name
* @return
*/
@RequestMapping(value = "/list", method = RequestMethod.GET)
@ResponseBody
public String getDetails(@RequestParam List<String> id) { 
return "Student id's are " + id;
}

5. Path Params Scenario:

Please find the below list of conditions which can be developed in Path params,

/**
* Path Parameter : Condition 1
* 
* Request -> http://localhost:9002/student/getdetails/1234
* Response -> Student id is 1234
* @param id
* @param name
* @return
*/
@RequestMapping(value = "/getdetails/{id}", method = RequestMethod.GET)
@ResponseBody
public String getUriPathDetails(@PathVariable("id") String id) { 
return "Student id is " + id;
}

6. Combination of both Path parameter and Query parameter:

Please find the below samples which can be developed in both Path params and Query params,

/**
* Combine both Path parameter and Query parameter
* 
* Request -> http://localhost:9002/student/getdetails/1234?name=CTG
* Response -> Student id is 1234
* @param id
* @param name
* @return
*/
@RequestMapping(value = "/getdetails/{id}", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public String getDetails(@PathVariable("id") String id, @RequestParam("name") String name, @RequestBody StudentInfo studentInfo) { 

System.out.println("StudentInfo city: "+ studentInfo.getCity());
System.out.println("StudentInfo: state "+ studentInfo.getState());
System.out.println("StudentInfo: country "+ studentInfo.getCountry());
System.out.println("StudentInfo: id "+ id);
System.out.println("StudentInfo: name "+ name);
return "Success";
}

Please find the GitHub link for code and YouTube video for live understanding.

GitHub: https://github.com/BuddiesCoolTechGuys/LetsDebugTech.git

YouTube: https://youtu.be/92MkjxDIcpM

Thanks!

Leave a Comment