To develop REStFul Services/ REST APIs using java SUN Microsystem released 'JAX-RS' API.
-> JAX-RS api having 2 implementations
1) Jersey (Sun Microsystems)
2) REST Easy (JBOSS)
Note: We can develop RESTFul Services using any one of the above implementation.
-> Spring framework also provided support to develop RESTFul Services using 'Spring Web MVC' module.
RESTFul Services Architecture
++++++++++++++++++++++++
-> We will have 2 actors in RESTful services
1) Provider / Resource
2) Consumer / Client
-> The application which is providing services to other applications is called as Provider or Resource application.
-> The application which is accessing services from other applications is called as Consumer or Client application.
-> Client application and Resource application will exchange data in an interoperable format (like XML & JSON).
Note: RESTful Services are used to develop B2B communications (No presentation logic, No view Resolver).
Develop First REST API Using Spring Boot
+++++++++++++++++++++++++++++++++
1) Create Spring starter application with below dependencies.
a) web-starter
b) devtools
2) Create RestController with Required methods.
Note: To represent java class as Rest Controller we will use @RestController annotation.
@RestController = @Controller + @ResponseBody
Note: Every RestController method should be binded to HTTP Protocol method.
Ex: @GetMapping, @PostMapping, @PutMapping & @DeleteMapping
3) Run the application and test it.
Note: To test REST APIs we will use POSTMAN tool (It is free).
Note: Download postman tool to test our REST API functionality.
+++++++++++++++++++++++++++++++++++++++++++
@RestController
public class WelcomeRestController {
@GetMapping("/welcome")
public ResponseEntity<String> getWelcomeMsg() {
String respPayload = "Welcome to Ashok IT";
return new ResponseEntity<>(respPayload, HttpStatus.OK);
}
@GetMapping("/greet")
public String getGreetMsg() {
String respPayload = "Good Morning..!!";
return respPayload;
}
}
+++++++++++++++++++++++++++++++++++++++++++
Note: GET Request will not contain Request Body to send data
-> We can use Query Params and Path Params to send data in GET Request.
-> Query Params & Path Params will represent data in URL directlry.
Query Params
+++++++++++
-> Query Params are used to send data to server in URL directly.
-> Query Params will represent data in key-value format.
-> Query Params will start with '?'.
-> Query Parameters will be seperated by '&'.
-> Query Parameters should present only at the end of the URL.
Ex: www.ashokit.in/courses?name=SBMS&trainer=Ashok
-> To read Query Parameters from the URL we will use @RequestParam annotation.
@GetMapping("/welcome")
public ResponseEntity<String> getWelcomeMsg(@RequestParam("name") String name) {
String respPayload = name + ", Welcome to Ashok IT";
return new ResponseEntity<>(respPayload, HttpStatus.OK);
}
URL : http://localhost:8080/welcome?name=Raju
Working with 2 Query Params in URL
++++++++++++++++++++++++++++
@RestController
public class CourseRestController {
@GetMapping("/course")
public ResponseEntity<String> getCourseFee(@RequestParam("cname") String cname,
@RequestParam("tname") String tname) {
String respBody = cname + " By " + tname + " Fee is 7000 INR";
return new ResponseEntity<>(respBody, HttpStatus.OK);
}
}
URL : http://localhost:8080/course?cname=JRTP&tname=Ashok
Path Parameter or URI variables
++++++++++++++++++++++++
-> Path Parameters are also used to send data to server in URL.
-> Path Params will represent data directley in URL (no keys).
-> Path Params can present anywhere in the URL.
-> Path Params will be seperated by / (slash).
-> Path Params should be represented in Method URL pattern (Template Pattern)
Ex: www.javads.in/courses/{cname}/trainer/{tname}
-> To read Path Parameters we will use @PathVariable annotation
@RestController
public class BookRestController {
@GetMapping("/book/{name}")
public ResponseEntity<String> getBookPrice(@PathVariable("name") String name) {
String respBody = name + " Price is 400 $";
return new ResponseEntity<>(respBody, HttpStatus.OK);
}
@GetMapping("/book/name/{bname}/author/{aname}")
public ResponseEntity<String> getBook(@PathVariable("bname") String bname,
@PathVariable("aname") String aname) {
String respBody = bname + " By " + aname + " is out of stock";
return new ResponseEntity<>(respBody, HttpStatus.OK);
}
}
URL-1 : http://localhost:8080/book/spring
URL-2 : http://localhost:8080/book/name/spring/author/rodjohnson
Q) When to use Path Params & Query Params ?
+++++++++++++++++++++++++++++++++++
-> To retrieve more than one record/resource we will use Query Params (filtering).
-> To retrieve specific/unique records we will use Path Params (single).
Comments
Post a Comment