Java Restful Web Services(JAX-RS) Using Jersy

05 Nov

This tutorial demonstrates how to build a Java Restful web service in Eclipse using Jersy plugin.

Download project

REST(Representational State Transfer) is an architectural style. This is not a standard whereas SOAP is standardized. RESTful web services are based on HTTP protocol and its methods PUT, GET, POST, and DELETE. They do not require XML SOAP messages or WSDL service definitions.

REST can be considered similar to a client-server architecture. There ‘s a REST server waiting for the requests from REST clients.

Requirements :

Main Topics
  1. Install Jersy plugin
  2. Create resource
  3. Create client
  4. Configure web.xml

1. Install Jersy plugin

Create new dynamic web project. Add  following jars from the downloaded Jersey zip file to WEB-INF/lib. Add them to project Build Path.

1. Create resource

package com.res;


//Sets the path to base URL + /hello
public class Hello {

// This method is called if TEXT_PLAIN is request
public String sayPlainTextHello() {
return "Hello Jersey";

// This method is called if XMLis request
public String sayXMLHello() {
return "<?xml version=\"1.0\"?>" + "<hello> Hello Jersey" + "</hello>";

// This method is called if HTML is request
public String sayHtmlHello() {
return "<html> " + "<title>" + "Hello Jersey" + "</title>"
+ "<body><h1>" + "Hello Jersey" + "</body></h1>" + "</html> ";


3. Create client

package com.client;


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.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;

public class client {

* @param args
public static void main(String[] args) {
ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
WebResource service = client.resource(getBaseURI());
// Fluent interfaces
// Get plain text
// Get XML
// The HTML

private static URI getBaseURI() {
return UriBuilder.fromUri("http://localhost:8440/Success").build();


4. Configure web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="" xmlns="" xmlns:web="" xsi:schemaLocation="" id="WebApp_ID" version="2.5">
<servlet-name>Jersey REST Service</servlet-name>
<servlet-name>Jersey REST Service</servlet-name>

web.xml is tricky!
[Extracted from :
The display name. It can be anything, but it’s

what goes first thing in the URL (browser address

line) after the protocol (http) and domain-

name:port-number. Note that I’m not using

anything that looks like a package name here. I

don’t even have to use the Eclipse project name.

Obviously, it cannot contain a space.

The servlet name. It can have spaces in it, but it’s

used in another place and the two must be identical.

The servlet name doesn’t show up outside this file,

but this file uses it to find the appropriate mapping


The servlet container class. This pertains to the

framework you’re using. Here, we’re using Jersey, so

we’re sure to name that class which comes out of

the user library (set of Jersey JARs) we used.

The servlet container class parameter name, as

dictated (in this instance) by the REST servlet

container or framework in use.

The value of the preceding parameter. It must be

the package name that contains at least and at most

one (JAXB) servlet (.java file). There may be more

than one such servlet, but if so, the other(s) must be

in a different Java package.

The servlet name must be identical to the one

already specified above. As noted, if there are two

servlets, there are two servlet definitions and two,

separate servlet mapping definitions.

The URL pattern. It starts with ‘/’ and ends with

‘/*’ and can be any legitimate directory-like

identifier (without spaces). It is the penultimate

element of the URL, just before the class name path.



        Jersey REST Service


de.vogella.jersey.first 1 Fun with REST com.sun.jersey.spi.container.servlet.ServletCo ntainer 1 Jersey REST Service /rest/* Fun with REST /fun/*

Posted by on November 5, 2012 in J2EE, Java


Tags: ,

3 responses to “Java Restful Web Services(JAX-RS) Using Jersy

  1. Takara-Kabuto.Com

    January 4, 2013 at 7:44 am

    My partner and I stumbled over here from a different
    page and thought I may as well check things out. I
    like what I see so i am just following you. Look forward to checking out your web page

  2. backlinks

    January 12, 2013 at 7:04 am

    Excellent pieces. Keep writing such kind of info on your page.
    Im really impressed by your blog.
    Hey there, You’ve done a fantastic job. I’ll certainly digg it and in my opinion suggest to my friends.
    I’m confident they will be benefited from this web site.

    • Rajitha

      January 12, 2013 at 9:43 pm

      Thank you for commenting this tutorial.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: