SPRING BOOT – PART2
[email protected]
INDEX
What is API?
What is REST?
Key elements of REST
Spring Boot Rest Services
Rest Controller
Request Mapping
CRUD Microservice with REST API
www.4kitsolutions.com
CHAPTER 1
WHAT IS REST API?
API
Application Programming Interface – is a kind of software interface through
which two programs or service can interact with each other
Ex- If we want to have an application that allows us to do create, read, edit
& delete operations. We can create an HTTP API that allows us to perform
these functions :-
http://localhost:8087/view_Courses
http://localhost:8087/create_Course?name=Automation
http://localhost:8087/update_Course?id= 4KCJ003&name=Automation
http://localhost:8087/delete_Course? id= 4KCJ003
www.4kitsolutions.com
REST
REST (REpresentational State Transfer) is a web standards
based architecture which uses HTTP Protocol for data
communication
In REST architecture, a REST Server simply provides access
to resources and the REST client accesses and presents the
resources
REST uses various representations to represent a resource like
Text, JSON and XML
www.4kitsolutions.com
KEY ELEMENTS OF REST
Resources – Let assume, a web application on a server has records of
several employees & URL of web application is http://demo.spring.com.
Now to get all employee record resource via REST, one can issue command
http://demo.spring.com/employee/4 which tells web server to provide
details of employee whose employee Id 4
Request Verbs – These describe what we want to do with the resource. A
browser issues a GET verb to get data. However, there are many other verbs
available including things like POST, PUT & DELETE.
In case of example http://demo.spring.com/employee/4, web browser is
issuing a GET Verb because it wants to get details of employee record
www.4kitsolutions.com
REST VERB
www.4kitsolutions.com
KEY ELEMENTS OF REST ….
Request Headers – These are
additional instructions sent with the
request. These might define the type
of response required or authorization
details like username etc.
Request Body - Data is normally
sent in request when a POST request
is made to the REST web service. In
a POST call, client tells web service,
that it wants to add a resource to the
server.
www.4kitsolutions.com
KEY ELEMENTS OF REST ….
Response Body – If we query web server via request
http://demo.spring.com/employee/1 , web server might return an XML/json
with all details of employee
Response Status codes – When we make an HTTP request, server
will respond with a code which corresponds to whether or not the request
was successful and how client should proceed. There are four different
levels of codes:
2xx = Success
3xx = Redirect
4xx = User error
5xx = Server error
www.4kitsolutions.com
IMPORTANT STATUS CODE
List of the most important status codes
Success codes:
200 - OK (the default)
201 – Created
202 - Accepted (often used for delete requests)
User error codes:
400- Bad Request (generic user error/bad data)
401 - Unauthorized (this area requires you to log in)
404 - Not Found (bad URL)
405 - Method Not Allowed (wrong HTTP method)
409 - Conflict (i.e. trying to create same resource with PUT request)
www.4kitsolutions.com
5 FUNDAMENTALS OF REST
Everything is resource – Image, file, data, video etc.
Every resource is identified by Unique Identifier
Use simple & uniform interface
GET -> Get resource
PUT -> Create & Update
DELETE -> Delete
POST -> Submit Data to resource
All communication done via representation (request &
response between client and server) like json, xml etc
Stateless – Every request will be independent request
www.4kitsolutions.com
API VS REST VS REST API
API
API is a set of definitions and protocols for building and integrating
application software. It’s sometimes referred to as a contract between
an information provider (producer) and an information user (consumer)
REST
REST is a set of architectural constraints
REST API
A Rest API also known as RESTful API is an application programming
interface (API or web API) that conforms to the constraints of REST
architectural style
www.4kitsolutions.com
RESTFUL WEB SERVICES
Example of Rest Web Services
JSON
or
XML
http://4kitsolutions.com/Courses
www.4kitsolutions.com
CHAPTER 2
SPRINGBOOT
REST SERVICES
BUILDING RESTFUL WEB SERVICES
Spring Boot provides very good support to building RESTful
Web Services
Spring Boot Starter Web dependency is used for building
RESTful Web Services
www.4kitsolutions.com
REST CONTROLLER
Spring 4.0 introduced @RestController, a specialized version of
controller which is a convenience annotation that does nothing
more than add @Controller & @ResponseBody annotations
and hence we no longer need to add @ResponseBody to all
the request mapping methods.
@RestController annotation informs the Spring application to
render the result back to the caller
www.4kitsolutions.com
REQUEST MAPPING
@RequestMapping annotation is used to define the Request URI to
access the REST Endpoints (data or resource)
We can define Request method to consume and produce object. The
default request method is GET.
OR
www.4kitsolutions.com
COURSE EXAMPLE
Create POJO for Courses -> Course.java
Create new controller class for Course
CourseController annotated with
@RestController &
GET API method annotated with
@RequestMapping
www.4kitsolutions.com
COURSE EXAMPLE ….
Starting SPRING BOOT Application
www.4kitsolutions.com
SPRING MVC FRAMEWORK &REST
Spring’s annotation-based MVC framework simplifies the
process of creating RESTful web services.
The key difference between a traditional Spring MVC
controller and the RESTful web service controller is the
way the HTTP response body is created.
While the traditional MVC controller relies on the View
technology, the RESTful web service controller simply returns
the object and the object data is written directly to the HTTP
response as JSON/XML.
www.4kitsolutions.com
SPRING MVC FRAMEWORK & REST
Spring MVC traditional workflow
www.4kitsolutions.com
SPRING MVC FRAMEWORK & REST
Spring 3.x MVC RESTful web services workflow
www.4kitsolutions.com
BUILDING COURSES CRUD MICROSERVICE
GET (Read Operation – get all details)
POST (Create Operation – create new entry)
www.4kitsolutions.com
POSTMAN – REST API TESTING
POSTMAN is an API Testing tool, which can be used to test REST APIs
Official Website -> https://www.postman.com/
www.4kitsolutions.com
TEST CRUD MICROSERVICE ....
POST (Create)
GET (Read)
www.4kitsolutions.com
COURSE CRUD ….
PUT (Update Operation)
DELETE (Delete Operation)
www.4kitsolutions.com
COURSE CRUD ….
PUT (Update)
DELETE (Delete)
www.4kitsolutions.com
THANK YOU
HAPPY LEARNING!!
[email protected]