Tuesday, July 06, 2010

Jersey Restful Web Service Example


TestJerseyRestWebService : This example shows to use the run time parameter while executing REST Webservices.

HelloWorldResources:

package testJerseyRestWebService;

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.MediaType;

//Please refer jersey-documentation-1.1.5-ea-SNAPSHOT-user-guide.pdf to attached project

//http://blog.iparissa.com/google-app-engine-jax-rs-jersey/

//The Java class will be hosted at the URI path "/helloworld"

@Path("/helloworld/{firstnumber}/{secondnumber}")

public class HelloWorldResources {

/*

// The Java method will process HTTP GET requests

@GET

// The Java method will produce content identified by the MIME Media

// type "text/plain"

@Produces("text/plain")

public String getClichedMessage(@PathParam("username") String userName) {

// Return some cliched textual content

return " Hello, "+userName +" +Welcome to the world of JAX-RS! PLAIN";

}

@GET

@Produces(MediaType.TEXT_XML)

public String getClichedMessageXML(@PathParam("username") String userName) {

// Return some cliched textual content

return " Hello, "+userName +" +Welcome to the world of JAX-RS! XML ";

}

@GET

@Produces(MediaType.TEXT_HTML)

public String getClichedMessageHTML(@PathParam("username") String userName) {

// Return some cliched textual content

return " Hello, "+userName +" +Welcome to the world of JAX-RS! HTML";

}

*/

@GET

@Produces(MediaType.TEXT_PLAIN)

public String getAddition(@PathParam("firstnumber") String firstNumber,@PathParam("secondnumber") String secondNumber) {

// Return some cliched textual content

System.out.println("Inside TEXT_PLAIN");

Integer ans = Integer.parseInt(firstNumber) + Integer.parseInt(secondNumber);

return " Addition of two values is : "+ans.toString();

}

@GET

@Produces(MediaType.TEXT_XML)

public String getAdditionXML(@PathParam("firstnumber") String firstNumber,@PathParam("secondnumber") String secondNumber) {

// Return some cliched textual content

System.out.println("Inside TEXT_PLAIN");

Integer ans = Integer.parseInt(firstNumber) + Integer.parseInt(secondNumber);

return " Addition of two values is : "+ans.toString();

}

@GET

@Produces(MediaType.TEXT_HTML)

public String getAdditionHTML(@PathParam("firstnumber") String firstNumber,@PathParam("secondnumber") String secondNumber) {

// Return some cliched textual content

System.out.println("Inside TEXT_PLAIN");

Integer ans = Integer.parseInt(firstNumber) + Integer.parseInt(secondNumber);

return " Addition of two values is : "+ans.toString();

}

}

appengine-web.xml:

xml version="1.0" encoding="utf-8"?>

<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">

<application>application>

<version>1version>

<system-properties>

<property name="java.util.logging.config.file" value="WEB-INF/logging.properties"/>

system-properties>

appengine-web-app>

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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

version="2.5">

<servlet>

<servlet-name>Jersey Web Applicationservlet-name>

<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainerservlet-class>

<init-param>

<param-name>com.sun.jersey.api.core.packagesparam-name>

<param-value>com.dclonline.jerseytestparam-value>

init-param>

<load-on-startup>1load-on-startup>

servlet>

<servlet-mapping>

<servlet-name>Jersey Web Applicationservlet-name>

<url-pattern>/resources/*url-pattern>

servlet-mapping>

<welcome-file-list>

<welcome-file>index.htmlwelcome-file>

welcome-file-list>

<session-config>

<session-timeout>30session-timeout>

session-config>

web-app>

Index.html:

DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<meta http-equiv="content-type" content="text/html; charset=UTF-8">

<title>Hello App Enginetitle>

head>

<body>

<h1>Hello App Engine!h1>

<table>

<tr>

<td colspan="2" style="font-weight:bold;">Available Servlets:td>

tr>

<tr>

<td><a href="resources/helloworld/1/2">TestJerseyRestWebServicea>td>

tr>

table>

body>

html>

No comments: