xxxxxxxxxx
Rest API:
Why we use Rest Api?
Front-End Technoligies can not directly connect with database so we use rest api to connect them.
We make rest apis and can use it with any front-end technology like angular, vuejs and also can use with
android applications.
In MVC we use frontend direct in backend so no need of rest apis.
An API basically work as third party that interacts two systems/languages.
Representational State Transfer (REST) is a architectural style in which we request and get response from server
in json, xml formats.We use JSon format most.
REST API uses HTTP methods for communication between both client and server side and most of us are familiar
with the HTTP verbs such as GET, POST, PUT or DELETE.
xxxxxxxxxx
Let's break it into two components:
1) RESTful
2) API
An API is an interface through which one program or web site talks to another.
They are used to share data and services, and they come in many different
formats and types.
A RESTful API is one of the many possible ways that programs, servers,
and web sites can share data and services. REST (Representational State Transfer
) describes the general rules for how the data and services are represented
through the API so that other programs will be able to correctly request and
receive the data and services that an API makes available
xxxxxxxxxx
1) REQUEST
2) RESPONSE
When we send request, we need to know the API
methods/endpoints that are available:
- read documentation about API methods.
- Swagger tool, that has API methods and descriptions
simple endpoint:
…school.com/api/students
Types of Requests:
GET -> Read data
POST -> Create/insert data
PUT -> Update data
DELETE-> Delete data
I send GET, POST, PUT, DELETE type of API requests to
API endpoint/method and get response.
ORDS (oracle data service) API -> HR Database
ORDS API has methods that we can send request to, and it sends
response with Data from HR database.
xxxxxxxxxx
Representational state transfer is a software architectural style that defines
a set of constraints to be used for creating Web services.
Web services that conform to the REST architectural style, called RESTful Web
services, provide interoperability between computer systems on the internet
REST (Representational State Transfer) is an architectural style for designing networked applications. It defines a set of principles and constraints that enable the development of scalable and efficient web services. A REST API (Application Programming Interface) is an implementation of a web service that adheres to the principles of REST.
RESTful APIs are widely used for building web services and are commonly used in client-server communication over HTTP. They are platform-independent, language-agnostic, and can be consumed by various clients, including web browsers, mobile apps, or other servers.
Web services that use REST architecture are called RESTful
services.
▪ RESTful systems communicates over HTTP protocol with HTTP
methods used by Web Browsers to transfer pages.
▪ Richardson Maturity Model
Leonard Richardson proposed the Richardson Maturity Model for
web APIs:
▪ Level 0: Define one URI, and all operations are POST requests to
this URI.
▪ Level 1: Create separate URIs for individual resources.
▪ Level 2: Use HTTP methods to define operations on resources.
▪ Level 3: Use hypermedia
xxxxxxxxxx
package com.howtodoinjava.rest;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;
import org.jboss.resteasy.spi.validation.ValidateRequest;
import com.howtodoinjava.exception.MyApplicationException;
@Path("/rest")
public class UserService
{
@Path("/users/{id}")
@GET
@ValidateRequest
public Response getUserBId ( @PathParam("id") String id ) throws MyApplicationException
{
//validate mandatory field
if(id == null)
{
throw new MyApplicationException("id is not present in request !!");
}
//Validate proper format
try
{
Integer.parseInt(id);
}
catch(NumberFormatException e)
{
throw new MyApplicationException("id is not a number !!");
}
//Process the request
return Response.ok().entity("User with ID " + id + " found !!").build();
}
}