Monday, May 25, 2015

@Delete Annotation

In this example we are going to use @DELETE Annotation. For this example we are going to have the following files:

  1. WEB.xml
  2. DeleteExample.java 

WEB.xml


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>com.tushar.jersey.Project</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
                                <servlet-name>jersey-serlvet</servlet-name>
                                <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer </servlet-class>
                                <init-param>
                                     <param-name>com.sun.jersey.config.property.packages</param-name>
                                     <param-value>com.tushar.jersey.Project</param-value>
                                </init-param>
                                <load-on-startup>1</load-on-startup>
                </servlet>
  <servlet-mapping>
                                <servlet-name>jersey-serlvet</servlet-name>
                                <url-pattern>/rest/*</url-pattern>
                </servlet-mapping>

</web-app>


DeleteExample.java


 package com.tushar.jersey.Project;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;

@Path("/deleteAnnotation")public class DeleteExample {

@DELETE
@Consumes("application/json")
@Produces("application/json")
@Path("{actor}")

public Response update(Movie movie) {
 String output = "This is an example of DELETE Annotation" + movie;
     return Response.status(200).entity(output).build();
}

}


Run the Server: 

Start the server and paste the following URL in the browser:                           

          http:// localhost:8080/com.tushar.jersey.Project/rest/deleteAnnotation/actor



Output:

@PUT Annotation

In this example we are going to use @PUT Annotation. For this example we are going to have the following files:

  1. WEB.xml
  2. PutExample.java 

WEB.xml


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>com.tushar.jersey.Project</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
                                <servlet-name>jersey-serlvet</servlet-name>
                                <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer </servlet-class>
                                <init-param>
                                     <param-name>com.sun.jersey.config.property.packages</param-name>
                                     <param-value>com.tushar.jersey.Project</param-value>
                                </init-param>
                                <load-on-startup>1</load-on-startup>
                </servlet>
  <servlet-mapping>
                                <servlet-name>jersey-serlvet</servlet-name>
                                <url-pattern>/rest/*</url-pattern>
                </servlet-mapping>

</web-app>


PutExample.java


 package com.tushar.jersey.Project;

import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;

@Path("putAnnotation") public class PutExample {

@PUT
@Path("{actor}")
public Response update(Movie movie) {
String output = "This is an example of PUTAnnotation" + movie;
     return Response.status(200).entity(output).build();
}

}


Run the Server: 

Start the server and paste the following URL in the browser:                           

          http:// localhost:8080/com.tushar.jersey.Project/rest/putAnnotation/actor



Output:

Generating JSON Response

In this example we are going to show generating JSON response

Steps:

1.      WEB.xml :

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xmlns="http://java.sun.com/xml/ns/javaee"
                xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
                id="WebApp_ID" version="2.5">
                <display-name>com.tushar.jersey.Project</display-name>
                <welcome-file-list>
                                <welcome-file>index.html</welcome-file>
                                <welcome-file>index.htm</welcome-file>
                                <welcome-file>index.jsp</welcome-file>
                                <welcome-file>default.html</welcome-file>
                                <welcome-file>default.htm</welcome-file>
                                <welcome-file>default.jsp</welcome-file>
                </welcome-file-list>

                <servlet>
                                <servlet-name>jersey-serlvet</servlet-name>
                                <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
                                <init-param>
                                                <param-name>com.sun.jersey.config.property.packages</param-name>
                                                <param-value>com.tushar.jersey.Project</param-value>
                                </init-param>
                                <init-param>
                                <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
                                                <param-value>true</param-value>
                                </init-param>
                                <load-on-startup>1</load-on-startup>
                </servlet>


                <servlet-mapping>
                                <servlet-name>jersey-serlvet</servlet-name>
                                <url-pattern>/rest/*</url-pattern>
                </servlet-mapping>

</web-app>





2.      Create Service class


package com.tushar.jersey.Project;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;

import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

@Path("/jsonResponse")
public class JSONResponse {

       @GET
         @Produces("application/json")
         public Response convertFtoC() throws JSONException {

              JSONObject jsonObject = new JSONObject();
              Double fahrenheit = 98.24;
              Double celsius;
              celsius = (fahrenheit - 32)*5/9;
              jsonObject.put("F Value", fahrenheit);
              jsonObject.put("C Value", celsius);

              String result = "@Produces(\"application/json\") Output: \n\nF to C Converter Output: \n\n" + jsonObject;
              return Response.status(200).entity(result).build();
         }

         @Path("{f}")
         @GET
         @Produces("application/json")
         public Response convertFtoCfromInput(@PathParam("f") float f) throws JSONException {

              JSONObject jsonObject = new JSONObject();
              float celsius;
              celsius =  (f - 32)*5/9;
              jsonObject.put("F Value", f);
              jsonObject.put("C Value", celsius);

              String result = "@Produces(\"application/json\") Output: \n\nF to C Converter Output: \n\n" + jsonObject;
              return Response.status(200).entity(result).build();
         }
      
}



2.      Execute the Code

       http://localhost:8080/com.tushar.jersey.Project/rest/jsonResponse

Output:














http://localhost:8080/com.tushar.jersey.Project/rest/jsonResponse/10


Output: