Introduction:
Everyone is saying Rest API. Sometimes people say only API to rest apis.RESTful API is for accessing blah blah service or to do blah blah functionality. In corporate world, fresher get confused when seniors give them task to create new api to call blah blah service or to do blah blah functionality.So what is this REST API?
- REST (REpresentational State Transfer) is an architectural style, and an approach to communications.Using this REST whatever functionality we create known as REST API.
- We can implement rest-api using various providers but usually people use Jersey and Spring .
- As per my choice personally I would like to use jersey which keep code neat and clean.
It completely depends on http protocol .REST implementation
is very easy and run in less memory
compared to SOAP.
Rest Support following parameter techniques to pass input
for our web service.
- Query/Request parameter(e.g. "http://localhost:8080/app?id=10&name=shashank")
- Path Parameter(e.g. "http://localhost:8080/app/id/100/name/shashank")
- Matrix Parameter(e.g. "http://localhost:8080/app;id=100;name=shashank")
- Form Parameter
- Header Parameter
REST supports sending data using XML , JSON, Plain Text etc.
Now the thing is
that what are the XML, JSON and Plain Text?
So we are going
to use JSON so I will not waste your time by explaining XML and Plain Text.
JSON- JSON is
JavaScript Object Notation which is very lightweight as compared to XML
and self-descriptive.While transferring it to Network it improves lot of performance.
and self-descriptive.While transferring it to Network it improves lot of performance.
e.g. =>
{
"Id": 100,
"Name":
"shashank"
}
Now we need
convertor to convert Object to JSON and
JSON to Object.So for that purpose we will use JACKSON.
- Front Controller is a servlet, which follows URL Pattern directory Pattern. It can even follow extensions Pttern but not recommended (does not support parameter passing.)
- All services or you can say api are classes ,every class must contain a unique path(no duplicate path)
- Every method inside the class must follow one type( GET or POST or PUT or DELETE)
- Webservice/api may return some data which is known as Entity and this entity can wrapped into Response object and sent to the client over network.
- When using Jersey common annotations are @Path, @GET, @POST, @PUT, @DELETE.
REST Architecture Workflow:
REST- Service Provider Implementation
- Create a New Dynamic Web Project(J2EE) : File-> New->Dynamic Web Project-> Enter -> Name -> Finish.
- Download and extract jersey-bundle.
- Add these jars into lib folder in an application: WebContent->WEB-INF->lib->paste here
- Configure Front Controller in web.xml,
- Don’t fear Front Controller is nothing but predefined class from jersey.You just need to give name with package name in web.xml
Front Controller class =
com.sun.jersey.spi.container.servlet.ServletContainer
Even if you do Ctrl+Shift+T in eclipse, It will
search ServletContainer class for you.
web.xml
<web-app>
<servlet>
<servlet-name>sample</servlet-name>
<servlet-class> com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>sample</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
Now We will create the web service or API for that we took
this much above boring theoretical knowledge –
Right click on src -> New -> class -> Enter class name and package
package com.app;
import javax.ws.rs.GET
import javax.ws.rs.Path
@Path(“hello”)
public class WebService{
@Path(“world”)
@GET
public String sayHello(){
return “Hello World”;
}
}
Run as Server and access request using as web
browser.
URL will be "http://localhost:8080/app-name/rest/hello/world"
Thank you…..!!!
Comments
Post a Comment