Saturday, April 7, 2012

Concepts of REST

What is REST?

REST- Representational State Transfer
The REST architecture involves client and server. When the client requests for the resource, which is exposed by the server, the server responses the representation. The representation of the resource can be in JSON,XML, plain text and HTML etc...

The resource can be identified with the meaningful URI.
For example, the user details whose age is between 25 to 50 are need to be fetched from the host . Then the resource URI can be designed as
http:///userdetails?from=25?to=50

The REST architecture depends on the HTTP methods. The REST uses the HTTP methods such as GET,PUT,POST,DELETE for performing the CRUD operations. The details of these operation are described below.

Pros:

- The applications can interchange the data using the RESTful web services irrespective of the language and platform used by the other application.
- The RESTful applications leave the room for the scalability of the application.
- It is light web services provider compared to JAX-WS which uses SOAP. The JAX-RS(Restful web services) uses only the standard HTTP methods. The advantages of REST over REST can be found at Advantages of REST over SOAP.

Cons:

- It has the limitation of using only the HTTP methods(GET,POST,PUT,DELETE). Complex operations are difficult to perform.
- On World Wide Web, clients can cache the responses. Which makes the client to receive the stale data.


HTTP MethodDescriptionJAX-RS annotation
GETGet method is used only to retrieve the information
represented by the resource(URI). Some times, the
GET method can return cache or stale data as because
GET request responses are cached by the browser to
reduce un necessary network calls
@GET is the JAX-RS annotation
equivalent for the HTTP GET method.
PUTPUT is used to create the resource at the specified URI. If the request URI does not exist then the resource can be created. If a resource is already exits at the request URI then the existing resource can be replaced with the requested.
The server responds with 201 - resource created,200 - OK, 204 - No content
@PUT is an equivalent JAX-RS annotation for HTTP PUT method
POSTThe URI at the POST request will handle the request. It is usually used for modifying a resource.@POST is an equivalent JAX-RS annotation for HTTP POST method
DELETEThe resource at the requested URI will be deleted. The server responds with the 200(OK) and similar responses if the request for delete is successful by the time of response by server.@DELETE is an equivalent JAX-RS annotation for HTTP DELETE method

The Restful web services tutorial with Jersey can be found at RESTful web services tutorial

Advantages of REST over SOAP


  • REST is simpler and light weight than SOAP.
  • REST can straight away provide the data in the JSON format. Where as in SOAP additional step of converting the XML to JSON is needed, which is an overhead. 
  • Any browse can act as the REST client, where as for SOAP services, SOAP client must be wrote to access the SOAP services.
  • SOAP uses the WSDL to expose the we services, which is complex. Where REST is simpler to use.
  • Transactions are supported in SOAP where as in REST it is not.
  • JAX-WS is the JAVA API for the SOAP services. JAX-RS is the java API for building the REST ful web services.

Tutorial of RESTful web services using the Jersey(JAX-RS 311 reference implementation) can be found at Restful web services tutorial.

More concepts and detailed explanation on Restful web services can be found at Concepts of REST

Thursday, April 5, 2012

Restful web services tutorial

Basics:
REST stands for Representational State Transfer.
JAX-RS is a Java programming language API for exposing the Restful web services according to the REST architecture style.
Jersey is the reference implementation of JAX-RS 311 specification.

Resource is an URI which uniquely identifies the resource.
Representation is the response provided by the resource.
Representation of the resource http://developersofjava.blogspot.in/2012/04/restful-web-services-tutorial.html is in HTML which will be rendered to the browser.

Restful web services works on the HTTP methods
GET- Retrieve a representation
PUT- Modify a representation
POST- Create a representation
DELETE- Delete a representation
The REST operations can be performed only on the above methods.

Java had exposed the APIs as part of the JAX-RS 311 specification. The runtime providers must implement this specification to provide the Restful web services. The Restful web services developer is only needed to be aware of the JAX-RS standards, not necessarily the details of the runtime provider of the JAX-RS.

Hello world Restful web services with Jersey(JAX-RS 311 reference implementation):

  1. Create a dynamic web project in your Java EE Eclipse IDE as shown in the below picture. 
  2. Jersey tutorial, restful web services tutorial,hello world with restful webservices
  3. Download the below shown Jersey libraries. If any of the below links did not work you can directly download them from http://jersey.java.net/
  4. Place the downloaded Jersey libraries in the WEB-INF/lib directory.

  5. Restful web services tutorial, jersey tutorial, hello world Jersey
  6. Create HelloWorldREST.java root resource class as shown below.


  7. package com.sai.doj.rootresources;
    import javax.ws.rs.GET;
    import javax.ws.rs.Path;
    import javax.ws.rs.Produces;
    import javax.ws.rs.core.MediaType;

    @Path("/helloworld")
    public class HelloWorldREST {

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String helloWorld(){
    return "Hellow world";
    }

    }

    @path - The relative path for this root resource class. This can be specified at the method level as well.
    @Produces - The type of the representation. The method helloworld produces text output. You can produce JSON,XML and HTML etc... as well.
    @GET - for the get request of the resource specified with the @path annotation, the @Get annotated method will respond.
  8. Configure the web.xml file as shown like below.
  9. Jersey tutorial,Restful web services tutorial

  10. Make the web achieve and deploy into the Tomcat. For your convince i have attached the source code and war file.

The URL to access the resource that you deployed is http://localhost:8080/HelloWorldJersy/rest/helloworld


http://localhost:8080/ - Domain where the Tomcat is is installed
HelloWorldJersy - Name of the war file or display-name
rest - where our REST services are published. This is configured in web.xml
helloworld - Name of the resource, which is specified with @path in root resource class.

If you are facing any issue in making it to work, you can comment with the necessary details in this section Jersey tutorial, i am ready to help.

Linkedlist vs Arraylist

LinkedList:
  • Each element/node in the LinkedList refers to its next element/node. 
  •  The elements in the LinkedList are accessed sequentially. i.e. the element at the specific index of the LinkedList cannot be accessed directly. 
  •  Insertion and deletion of the elements at any position in the LinkedList is preferment than ArrayList.
  • Random access of the elements in the LinkedList is not possible.
ArrayList:
  • Random access of the elements is possible. eg: the element at the index 5 can be accessed as list.get(5) where as in LinkedList it is not possible.
  • Insertion and deletion of the elements in the middle is not performant compared to LinkedList. In order to insert an element at some position, it shifts the elements to its right.


Use LinkedList, only if the requirement is to insert/delete elements in the middle of the List when iterating over the list. If not in most of the cases the ArrayList can be opted blindly.

The LinkedList data structure details can be found here

The restful web services tutorial can be found at restful web services tutorial
The hello world with Jersey can found hello world with Jersey