RESTAPI Notes
RESTAPI Notes
RESTAPI Notes
Webservices
============
-> If one application is communicating with another application then those apps are
called as distributed applications.
====================
What is REST API ?
=====================
=> REST API means an application which provides business logic to other
applications through internet.
======================
REST API Architecture
=======================
1) Provider / Resource
2) Consumer / Client
=> Provider means the application which is providing business services to other
applications.
=> Consumer means the application which is accessing business services from other
applications.
Ex: MakeMyTrip & IRCTC
Note: We will use JSON to exchange data between provider & consumer.
=====
Task
=====
1) What is HTTP
2) HTTP methods & Purpose of those methods
3) Http Status Codes
=================
What is HTTP
=================
1) Http Request
2) Http Response
3) Http Methods
========================
HTTP Request Structure
========================
=============================
HTTP Response Structure
=============================
==============
HTTP Methods
==============
===================
HTTP Status Codes
===================
200 - OK
=======================================
Developing REST APIs using Spring Boot
=======================================
1) Provider Development
2) Consumer Development
@GetMapping
@PostMapping
@PutMapping
@PatchMapping
@DeleteMapping
@RequestParam
@PathVariable
@RequestBody
=================
HTTP GET Request
=================
1) Query Parameters
2) Path Parameters
Ex-1 : http://localhost:8081/welcome?name=raju
Ex-2: : http://localhost:8081/greet/john
===================================================================
@RestController
public class DemoRestController {
@GetMapping("/greet/{name}")
public String getGreetMsg(@PathVariable("name") String name) {
String msg = name + ", Good Morning..!!";
return msg;
}
@GetMapping("/welcome")
public String getWelcomeMsg( ") String name) {
String msg = name + ", Welcome to REST API";
return msg;
}
}
=============================================================================
@RestController
public class CustomerRestController {
==================
HTTP Post Request
==================
==================
HTTP PUT Request
==================
==================
HTTP DELETE Request
==================
==================
What is Swagger ?
==================
=> After running the application use below url to access swaager-documentation
URL : http://localhost:8081/swagger-ui/index.html/
========================================================================
Assignment -1 : Develop Spring Boot REST API to perform Crud operations with
Database table using Spring Data jpa and test it using Swagger documentation.
Assignment - 2 : Develop Ticket Booking Rest api (IRCTC api) with below operations.
Use H2 DB for saving data.
operation-1 : book-ticket
operation-2 : get-ticket using ticket-id
operation-3 : get-all-tickets
input : passenger-data
- name
- dob
- gender
- doj
- from
- to
- trainNum
output : ticket-data
- ticketId
- ticketStatus
- trainNum
- from
- to
- doj
- name
----------------------------------------------------------------------------
===================
What is xml ?
===================
----------
syntax:
----------
<person>
<id>101</id>
<name>Ashok</name>
</person>
============================================
Dealing with xml data in java applications
============================================
=> Upto java 8v we have JAX-B api in jdk to deal with xml files in java.
=> Using JAX-B api we can convert java object to xml and vice versa.
jax-b
java obj <---------> xml data
Note: From java 9 onwards jax-b api is not part of JDK software.
=> If we want to deal with xml data in java applications, we need to add dependency
=> To deal with xml data in spring boot rest api we need to add below dependency in
pom.xml file
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
</dependency>
=> Below is the rest controller method which supports both xml and json response
@RestController
public class ProductRestController {
@GetMapping(
value = "/product",
produces = {"application/xml" , "application/json"}
)
public ResponseEntity<Product> getProduct() {
Product p = new Product(101, "Monitor", 1000.00);
return new ResponseEntity<>(p, HttpStatus.OK);
}
}
Note: The above method returning Object that means it is loosely coupled with xml
and json formats.
=> Consumer should send Accept header in the request to decide response format.
Accept = appliction/json
Accept = application/xml
Note: Based on the Accept header value given by consumer response will be sent from
the provider.
===============================
what is Content-Type header ?
===============================
=> It is used to represent in which format consumer sending request body data to
provider.
@PostMapping(
value="/product",
consumes = {"application/xml", "application/json"},
produces = "text/plain"
)
public ResponseEntity<String> addProduct(@RequestBody Product p){
System.out.println(p);
//logic
return new ResponseEntity<String>("product added", HttpStatus.CREATED);
}
-------------------------------------------------------------
=============================================================
2) What is Intereoperability
- Provider
- Consumer
11) @RestController
12) ResponseEntity
15) @RequestBody
18) POSTMAN
19) Swagger
=========
Consumer
==========
1) RestTemplate (outdated)
2) WebClient
3) FeignClient
=> WebClient supports both sync & async communication (spring 5.x)
=> After sending request to provider if consumer is waiting for the response then
it is called as Synchronus communication.
===================================
What is Asynchronus Communication ?
===================================
=> After sending request to provider if consumer is not waiting for the response
then it is called as asynchronus communication.