0% found this document useful (0 votes)
12 views21 pages

Rest Api

The document discusses REST architecture and how communication happens between a provider and consumer using HTTP. It describes HTTP methods like GET, POST, PUT, DELETE and status codes. It also discusses JSON and enabling Swagger UI in Spring Boot REST applications.

Uploaded by

varun varun
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views21 pages

Rest Api

The document discusses REST architecture and how communication happens between a provider and consumer using HTTP. It describes HTTP methods like GET, POST, PUT, DELETE and status codes. It also discusses JSON and enabling Swagger UI in Spring Boot REST applications.

Uploaded by

varun varun
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

===================

REST Architecture
====================

1) Provider / Resource
2) Consumer / Client

Provider: The application which is giving services to other applications is


called as Provider application.

Consumer : The application which is accessing services from other


applications is called as Consumer application.
=============================================================
How communication will happen between Provider &
Consumer ?
=============================================================

-> HTTP protocol will act as mediator between Consumer and Provider

-> Consumer and Provider will exchange data in the form XML / JSON

Note: XML and JSON are intereoperable.


==============
HTTP Protocol
==============

1) Http Request
2) Http Response
3) HTTP Methods
4) HTTP Status Codes

=> HTTP will act as mediator between Client and Server


=> HTTP is stateless protocol (can't remember previous requests)
==============
HTTP Methods
==============

=> Every REST API method should be mapped to HTTP Method.

GET --> To get resource/data from server

POST --> To insert/create record at server

PUT --> To update data at server

DELETE --> To delete data at server


==================
HTTP Status Codes
===================
-> When client send request to server then server will process that request and
server will send response to client with status code.

100 - 199 (1xx) ---> Information

200 - 299 (2xx) ---> Success (OK)

300 - 399 (3xx) ---> Redirection

400 - 499 (4xx) ---> Client Error

500 - 599 (5xx) ---> Server Error


=============
HTTP Request
==============

-> HTTP request contains below parts

1) Request Line (Request Type + URL)

2) Request Header (metadata)

3) Request Body (Payload)


1>Request Header:-- It provides data in key=value format (both are String
type).
=>Request header also called as Header Parameters.
=>These are used to provide instructions to server (application/Controller).
Ex:-- Content-Type, Accept, host, Date, Cookies…
Content-Type : This header will represent in which format client sending data
to server in request body

Accept : This header will represent in which format client expecting


response from server
2>Request Body:-- To send large/bulk data like objects and collections
data in the form of JSON/XML.
=>Even supported large text (Raw data).
=>Spring boot enables JSON conversion by default, but not XML (no JAXB API).

3>Request Parameter:-- To pass primitive data (like id, name, codes … etc)
as input, request parameters are used.
=>format looks like url?key=val&key=val…
=>Both key and value are String type by default.
Difference between PUT and Patch :
PUT :-- It indicates modify complete (full) data, based on
Identity (ID-PK).
PATCH :-- It indicates modify partial (few data, based on
Identity (ID-PK).
Request Parameter: Format:--
@RequestParam(
value="key",
required=true/false,
defaultValue="-value-")
DataType localVariable
====================================
JSON (Java Script Object Notation)
====================================
=> JSON is used to represent data in key-value format
=> JSON is universal format to exchange data over internet
Synax:
{
"id" : 101,
"name" : “Suresh",
"gender" : "Male",
"phno" : 463413
"address" : {
"city" : “Mysore",
"state" : “Karnataka"
}
}
@RestController : To represent java class as Distributed Component

@RestController = @Controller + @ResponseBody

@GetMapping : Map the method to HTTP GET Request

@PostMapping : Map the method to HTTP POST Request

@RequestBody : To read payload from HTTP Request Body

ResponseEntity : To set custom HTTP Status Code in Response


If we apply @RestController over class internally it will apply
@ResponseBody
that converts data into JSON Format.
Enable Swagger UI in Spring Boot ReST Application:--
=>Compared to all other tools Swagger is a RichSet API provides dynamic UI based
on code written for Rest Controller with common Paths.

Step#1:- Add below dependencies in pom.xml


<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>
Flow Meaning
->Docket () =>Create Docket

->select () =>Choose Rest classes

->apis(basepackage()) =>Classes are in package

->paths (regex()) =>Having common path

->build() =>Create final output


http://localhost:8094/swagger-ui.html#/

You might also like