Thursday, 23 August 2012

WebService using RESTeasy

Webservice using RESTeasy is too simple. For creating the simple application we have to use the maven plugin or you can download the jar file and keep it inside the lib folder of the project.

First create a Dynamic Web Project and give any name you would like and then convert the project to a maven project and add the below dependency to the pom.xml.

pom.xml


<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>RestEasy</groupId>
<artifactId>RestEasy</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>RestEasy</name>
<description>RestEasy</description>
<dependencies>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>2.2.1.GA</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

Once you have done that then we need to create a class file with the name RestEasyTest.java. In this class file, @Path you need to specify for which this class need to be called as a mapping. then you need to create a method name showMessage and mention for what kind of call this method should respond. In the below example we are using the @Get. Now we need the parameter passed from the url to use in the method so we are using the {param} which will take the rest of the url after the "/test" and store it inside the string msg. Once this method is called we are returning the object of the Response.


RestEasyTest.java

package com.lnt.rest;

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

@Path("/test")
public class RestEasyExample {

@GET
@Path("/{param}")
public Response showMessage(@PathParam("param") String msg) {

String result = "Restful example : " + msg;

return Response.status(200).entity(result).build();

}

}


Configuration of the project to use the resteasy servlets are done at the web.xml



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_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>RestEasy</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>

<!-- Auto scan REST service -->
<context-param>
<param-name>resteasy.scan</param-name>
<param-value>true</param-value>
</context-param>

<!-- this need same with resteasy servlet url-pattern -->
<context-param>
<param-name>resteasy.servlet.mapping.prefix</param-name>
<param-value>/rest</param-value>
</context-param>

<listener>
<listener-class>
org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class>
</listener>

<servlet>
<servlet-name>resteasy-servlet</servlet-name>
<servlet-class>
org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>resteasy-servlet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>


The listener org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap make sure that the REST services are registed and listened.

we are also providing the  esteasy.servlet.mapping.prefix as "/rest" as the resteasy url pattern uses the /rest/*.


Now start the server and hit the link as

"http://localhost:8080/RestEasy/rest/test/hello%20prajith"

Result would be : Restful example : hello prajith

No comments:

Post a Comment