REST API Development
REST API Development
What is Intereoperability
What is HTTP Protocol
HTTP Methods
HTTP Status Codes
HTTP Request Structure
HTTP Response Structure
Working with XML and JAX-B
Working with JSON and Jackson
+++++++++++++++++++++++++++++++
How to develop REST API using Java
+++++++++++++++++++++++++++++++++
-> To develop RESFul Services/ REST APIs using java SUN Microsystem released 'JAX-
RS' API
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
+++++++++++++++++++++++++
1) Provider / Resource
2) Consumer / Client
-> 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 intereoperable
format (like XML & JSON)
request
client app <------------------------> resource app
response
Note: RESTful Services are used to develop B2B communications (No presentation
logic, No view Resolver)
+++++++++++++++++++++++++++++++++++
Develop First REST API Using Spring Boot
+++++++++++++++++++++++++++++++++++
a) web-starter
b) devtools
Note: To test REST APIs we will use POSTMAN tool (It is free)
+++++++++++++++++++++++++++++++++++
@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 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";
}
}
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 should be represented in Method URL pattern (Template Pattern)
Ex: www.ashokit.in/courses/{cname}/trainer/{tname}
@RestController
public class BookRestController {
@GetMapping("/book/{name}")
public ResponseEntity<String> getBookPrice(@PathVariable("name") String name)
{
@GetMapping("/book/name/{bname}/author/{aname}")
public ResponseEntity<String> getBook(@PathVariable("bname") String bname,
@PathVariable("aname") String aname) {
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)
################
What is Produces
#################
@RestController
public class ProductRestController {
@GetMapping(
value = "/product",
produces = { "application/xml", "application/json" }
)
public ResponseEntity<Product> getProduct() {
@GetMapping("/products")
public ResponseEntity<List<Product>> getProducts(){
##############################
Working with HTTP POST Request
#############################
-> To bind Rest Controller method to POST request we willl use @PostMapping
-> To read data from Requet body we will use @RequestBody annotation
-> "Content-Type" header represents in which format client sending data in request
body.
@Data
@XmlRootElement
public class Book {
}
---------------------------
@RestController
public class BookRestController {
@PostMapping(
value = "/book",
consumes = { "application/json", "application/xml" }
)
public ResponseEntity<String> addBook(@RequestBody Book book) {
System.out.println(book);
// logic to store in DB
----------------
{
"id" : 101,
"name" : "Java",
"price" : 450.00
}
------------------------
-> produces attribute represents in which formats Method can provide response data
to clients
-> consumes attribute represents in which formats Method can take request data from
clients
-> Accept header represents in which format client expecting response from REST API
-> Content-Type header represents in which format client is sending request data to
REST API
Note: We can use both Consumes & Produces in single REST API method.
++++++++++++++++++++++++++++++++++++++++++++++++++
Requirement : Develop IRCTC REST API to book a train ticket
++++++++++++++++++++++++++++++++++++++++++++++++++++
-> To develop any REST API first we have to understand the requirement
@Data
public class PassengerInfo {
@Data
public class TicketInfo {
@RestController
public class TicketRestController {
@PostMapping(
value = "/ticket",
produces = {"application/json"},
consumes = {"application/json"}
)
public ResponseEntity<TicketInfo> bookTicket(@RequestBody PassengerInfo
request){
System.out.println(request);
-------------------------------
{
"name" : "Ashok",
"phno" : 12345678,
"jdate" : "05-08-2022",
"from" : "hyd",
"to" : "pune",
"trainNum" : 8574
}
{
"ticketId": 1234,
"pnr": "JLJL6868",
"ticketStatus": "CONFIRMED"
}
#################
HTTP PUT Request
#################
-> PUT Request can take data in URL and in Request Body
@PutMapping("/ticket")
public ResponseEntity<String> updateTicket(@RequestBody PassengerInfo
request){
System.out.println(request);
//logic to update ticket
return new ResponseEntity<>("Ticket Updated", HttpStatus.OK);
}
####################
HTTP DELETE Request
####################
-> DELETE Request can take data in URL and in Request Body
@DeleteMapping("/ticket/{ticketId}")
public ResponseEntity<String> deleteTicket(@PathVariable("ticketId") Integer
ticketId){
//logic to delete the ticket
return new ResponseEntity<>("Ticket Deleted", HttpStatus.OK);
}
------------------------------------------
What is RestController ?
REST Controller Methods
@GetMapping
@PostMapping
@PutMapping
@DeleteMapping
Query Params
Path Params
Request Body
Response Body
@RequestParam
@PathVariable
@RequestBody
produces
consumes
Accept
Content-Type
Message Converters
ResponseEntity
Ans) Yes, we can do it but not recommended. We need to follow HTTP Protocol
standards while developing REST API.
+++++++++
Swagger
+++++++