Technical Section.

Interview Questions & Tutorial on Jersey REST API

What is rest ? 
Rest is representational state transfer and it uses HTTP protocol for communication. It is working on the top of HTTP protocol.Here component is a resource  and accessed by standard  HTTP protocols. It is architectural style representation of HTTP. Each resource is identified by URI. It supports  both XML and JSON. It is used for making single page web apps

Commonly used HTTP method
Get -> It is used for accessing data.It is a read only operation
Post -> It is used to create new data like create new object .It is read and write both
Put -> It is used to update if data is not present then used to create it

Delete -> It is used delete the data with given id or some identifier.

What is a web service?
It is collection of open protocols used for exchanging data between systems.

Different ways to represent a resource ?
JSON
XML
HTML FORMS(parameter passing)

What is content in REST?
Rest treats every content as a resource .It can be file, image, text in html pages.

What is messaging in rest
Client send data as request and server send back as a response in form of message contains all metadata about message

What are major components of HTTP request
VERB - like put post, delete , get
URI - uniform resource identifier
HTTP version - like 1.1
Response header - metadata of header like content type - application/json or accept
Request body - message content

What are major components of HTTP response
HTTP version - like 1.1
status/response code - 200 or 404
Response header - metadata of header like content type - application/json or accept
Request body - message content.

What is statelessness in rest?
It does not keep client state on the server
It treats every request as new request 

What is jersey?
It is the implementation of JAX RS in java.

Difference between put and post?
Post is used to create a new object each time
Where put used for updating information of particular object if it is not present then it will create it.

Implementation In Java
This Project is made in Eclipse using java 7 . I have use maven as build tool for this.

Here is the directory structure of eclipse project.



Here is pom.xml file which includes the dependency required for jersey 

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.jersey</groupId>
<artifactId>exercise-services</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>exercise-services</name>
<build>
<finalName>exercise-services</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<inherited>true</inherited>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>${jersey.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<!-- use the following artifactId if you don't need servlet 2.x compatibility -->
<!-- artifactId>jersey-container-servlet</artifactId -->
</dependency>
<!-- uncomment this to get JSON support -->
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
</dependency>
</dependencies>
<properties>
<jersey.version>2.2</jersey.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

</project>

<groupId>org.glassfish.jersey.media</groupId> 
this enables media support JSON type data.

Implementation of standard protocols 
important annotations used in JAX-RS :

@Path("activities")
    This annotation is used for accessing data of requested  URI
This is used both on class and methods.

@Consumes(MediaType.APPLICATION_JSON)

    It represents what type of data input you are providing to you application.

@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
    It represents what type of data you application is producing on UI. It may be simple HTML, image pdf etc.

@PathParam("activityId")

It is used to bind the any parameter to java variable. Here activityId provided by any parameter from UI and it is accessible in your requested URI.

@GET 
   It is used for getting data on UI
@POST  
   It is used for creating new data .Like submitting a new form
@PUT
  It is used for updating data for particular id if this id does not exist then some logic perform for creating new data.
@DELETE
  It is used to delete data for mentioned ID.

@PUT and @DELETE are both idempotent in nature because they can not be changed 


Use POSTMAN extension in chrome and RESTClient in firefox browser to test REST calls.

Implementation of @GET
Get method is used to display the data on the UI.
it used @get annotation and having media type of 
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })

URI will be localhost://7070/exercise-services/webapi/activities.
Here i am not providing any @Path for method so by default it will choose @GET with no no path


Here activityRepository is the Interface having definition of method used by @GET annotation.









Using postman with @GET annotation
 

URI in the broswer
 

ActivityRepositoryStub is the implementation of method used in Interface.



This method findAllActivities() returning the list containing two activities.

Retrieve data by ID
If you want to retrieve data by ID then URI will be  localhost://7070/exercise-services/webapi/activities/123





 
Here we are using the @PathParam("activityIDMod")  annotation for binding the parameter with the java variable.
Response class is used to to add some validations for checking status of response generated 


Implementation of @POST

If you are using post method and want to save object of particular type then you can pass that bean name to argument of that method. 




here is the Activity class by using @XmlRootElement on the class and  @XmlElement(name="id") on the getter method
Activity class is 


 
 
Implementation of @PUT


The main difference between put and post is that 
@POST is used to create a new object each time where @PUT used for updating information of particular object if it is not present then it will create it.
same as post annotation  you can use @PUT
It's URI will be  localhost://7070/exercise-services/webapi/activities/123

@PUT
@Path("{activityId}")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })






 
 

Implementation of @DELETE
Same annotations as used in the post method You have to use these below annotations for delete annotation.It's URI will be  localhost://7070/exercise-services/webapi/activities/123
@DELETE
@Path("{activityId}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })

  and you have to get id from UI to delete an object then use 

@PathParam("activityId") 



Comments