RESTful WebService Tutorial

Representational State Transfer (REST) is a style of software architecture for distributed systems such as the World Wide Web. REST has emerged as a predominant Web service design model.

We will be building a REST application using Apache tomcat , Jersey , Eclipse .
Below is the code for making a dummy application for Restful WebService using Jersey.

To begin with first set up all the jars required for the application.  Click the below link to download all the jars.

After that  create a client for making a hit to the app.
Create a class like JerseyGetClient.java and paste the below code inside it :


import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;

import com.sun.jersey.api.client.WebResource.Builder;

public class JerseyGetClient {
public static void main(String arg[]){
Client client =Client.create();
String url ="http://localhost:8085/REST-WSproject/rest/json/metallica/get";
WebResource resource = client.resource(url);
Builder builder = resource.accept("application/json");
ClientResponse response = builder.get(ClientResponse.class);
    
if (response.getStatus() != 200) {
throw new RuntimeException("Failed : HTTP error code : "
+ response.getStatus());
}
    String output = response.getEntity(String.class);
    System.out.println("Output from Server .... \n");
System.out.println(output);
}
}

After this create a class to process the data. I have named it as JSONService . Check for the annotation inside this class.

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

@Path("/json/metallica")
public class JSONService {
@GET
@Path("/get")
@Produces(MediaType.APPLICATION_JSON)
public Track getTrackInJSON(){
Track track=new Track();
track.setSinger("Akon");
track.setTitle("UnForgettable");
return track;
}
}


Create a POJO class . I have named it as Track.

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Track {
private String singer;
private String title;

public String getSinger() {
return singer;
}

public void setSinger(String singer) {
this.singer = singer;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

@Override
public String toString() {
return "Track [title=" + title + ", singer=" + singer + "]";
}
}

Now it's time to modify the web.xml  to configure the Jersey settings. Download the web.xml  contents from  the below link and paste inside your web.xml

https://sites.google.com/site/balonideepak/web.xml?attredirects=0&d=1



That's all .....cheers!!!!!

Click the link below to download the code and jars required for the project.

https://sites.google.com/site/balonideepak/src.zip?attredirects=0&d=1

https://sites.google.com/site/balonideepak/lib.zip?attredirects=0&d=1





Brother website for healthcare : http://delighthealthcare.in/


Comments

Popular posts from this blog

Arrays.sort() and Collections.sort() NullPointerException

org.xml.sax. SAXParseException: Element type "web-app" must be declared.

Sentimental Analysis : PART 1