Wednesday, October 15, 2014

How to build RESTful Web Service with Java using JAX-RS and Jersey


How to build RESTful Service with Java using JAX-RS and Jersey

Scope : Build a simple RESTful web service with using JAX-RS and Jersey.


Objective : Build an limited currency conversion RESTful service with using JAX-RS and Jersey

Requirements:
  • Eclipse JAVA EE IDE
  • JDK 1.6
  • ApacheTomcat - 6.0.26
  • The below 3 jersey related jars you have to download.
    • asm-3.3.1.jar
    • jersey-bundle-1.14.jar
    • json.jar

Step 1: Create a dynamic web project In eclipse and named as "RestCurrencyConvertService”.

File > New > Dynamic Web Project (Select the Dynamic web module version 2.5)

Add the downloaded jars to the project build path.


Step 2: Create the service "CurrencyConvertService" class under the "com.prem.restservice" package.

/RestCurrencyConvertService/src/com/prem/restservice/CurrencyConvertService.java


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package com.prem.restservice;

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;

@Path("CurrencyConvert")
public class CurrencyConvertService {

 @Produces(MediaType.TEXT_XML)
 @Path("/Convert/{from}&{to}&{amount}")
 @GET
 public String convertCurrency(@PathParam("from") String from,
   @PathParam("to") String to, @PathParam("amount") double amount) {

  // Small logic
  if (from.equalsIgnoreCase("USD") && to.equalsIgnoreCase("LKR")) {
   return "<result>" + Double.toString(amount * 130.65) + "</result>";
  } else if (from.equalsIgnoreCase("LKR") && to.equalsIgnoreCase("USD")) {
   return "<result>" + Double.toString(amount * 0.01) + "</result>";
  } else {
   return "<result>Invalid Currency Code...</result>";
  }
 }
}

Step 3: Add the servlet to the "web.xml" file.

The sample "web.xml" file as below.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?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"
 id="WebApp_ID" version="2.5">
 <display-name>RestCurrencyConvertService</display-name>
 <welcome-file-list>
  <welcome-file>index.html</welcome-file>
 </welcome-file-list>
 <servlet>
  <servlet-name>CurrecnyConvert</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.prem.restservice</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
  <servlet-name>CurrecnyConvert</servlet-name>
  <url-pattern>/Convert/*</url-pattern>
 </servlet-mapping>
</web-app>

That's all folks... 

Step 4: Now the time to execute and see the result.... 

Right click on the "RestCurrencyConvertService" project and go to Run As - Run on Server.

You able to see the below screen when you paste the below URL to browser and enter.
http://localhost:8181/RestCurrencyConvertService/Convert/CurrencyConvert/Convert/USD&LKR&100





http://localhost:8181 - The "ApacheTomcat - 6.0.26" server runs on the 8181 port at my local                                             machine
/RestCurrencyConvertService - Display name in the "web.xml" file
/Convert - The url pattern defined in the "web.xml"
/CurrencyConvert - @Path("xxxxxxxx") defined in the class level
/Convert - @Path("xxxxxxxx/{from}&{to}&{amount}") defined in the method level.
/USD&LKR&100 - @Path("/Convert/{xxx}&{yyy}&{zzz}") defined in the method level.

 In this example xxx = USD, yyy = LKR and zzz = 100. 

Hope its clear...



No comments:

Post a Comment