Skip to main content

Restful API for Beginners

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.
Rest follows client-server architecture with Front Controller Design Pattern.
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.

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.
  1. 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.)
  2. All services or you can say api are classes ,every class must contain  a unique path(no duplicate path)
  3. Every method inside the class must follow one type( GET or POST or PUT or DELETE)
  4. 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.
  5. When using Jersey common annotations are @Path, @GET, @POST, @PUT, @DELETE.

REST Architecture Workflow:

REST- Service Provider Implementation
  1. Create a New Dynamic Web Project(J2EE) : File-> New->Dynamic Web Project-> Enter -> Name -> Finish.
  2. Download and extract jersey-bundle.
  3. Add these jars into lib folder in an application: WebContent->WEB-INF->lib->paste here
  4. Configure Front Controller in web.xml,
  5. 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

Popular posts from this blog

Exception Propagation and throw-throws keyword

Exception Propagation: Sending exception from one method to another method is called exception propagation Here propagation is nothing but sending. So guys what is the situation when we are going to implement this propagation concept? Answer to this question is when we throw an exception, if it is not caught in that method it is propagated to another method and interesting thing is that we don’t need to do anything in this process we just need to throw an exception that’s it. Rule: If the propagated exception is checked exception then not only current method developer but also method’s caller method developer should caught or report that checked exception else it leads to same compiler exception….CE: Unreported Exception So according to that basic rule is “If checked exception is raised the method either directly by using throw keyword or indirectly by a method call it must be caught or reported. void m1() throws ClassNotFoundException{ ...

Apache Solr Search Framework

Introduction: SOLR stands for Searching On Lucene with Replication . Its main functionalities are indexing and searching. Solr is an open source enterprise search server/ web application Created by Yonik Seeley in 2004. In 2012 4.0 version with Cloud support. Solr uses lucene search library and extends it. Solr exposes lucene Java API's as RESTful services. Terminology: Solr Instance: In Apache Solr, a Solr Instance is an instance a Solr running in the JVM. In Standalone mode, Solr contains only one instance. Solr Core: In Apache Solr, a Solr Core is also known as simply “Core”. In other words, a Solr Core = an instance of Apache Lucene Index + Solr Configuratio. Indexing: In Apache Lucene or Solr, Indexing is a technique of adding Document’s content to Solr Index so that we can search them easily. Apache Solr uses Apache Lucene Inverted Index technique to Index it’s documents. That’s why Solr provides very fast searching feature. Document: Solr...