Computing Magazine

Creating Rest Services With Rest Easy In Web Application

Posted on the 01 May 2013 by Abhishek Somani @somaniabhi
RestEasy is implementation of JAX-RS API created by JBoss. This post is about how to create and expose rest services in your existing web application. First of all , add the dependency in your pom.xml for RestEasy.If you are not using jboss[jboss AS 7.1 is shipped with rest easy] as your application server , remove Scope tag from this.
Now create a root rest service. By default it returns Empty set of classes in getClasses method which means whole web Application will be scanned to find jax rs annotations.
package rest;

import java.util.Set;


public class RootRestService extends Application{
	public Set> getClasses()
		return super.getClasses();

This is our sample Rest Class .
package rest;
import java.util.HashMap;
import java.util.Map;


public class RestService {

	HttpClient cl = new HttpClient();
	public Response test() {
		return Response.status(Status.OK).entity("working").build();

Now in your web.xml , you have to define a servlet to dispatch your request to rest service. Provide url pattern for the dispatcher. in our case it is /rest/* , accordingly you have to define the prefix in context parameter . So if the url pattern is /rest/* then prefix will also be /rest.If you map the dispatcher to /* pattern , then you will not be able to access other resources in your web app like html ,css,jsp ,servlets etc . because every request will go through the rest easy dispatcher and you will get 404 error .So ,it's better to have a separate prefix for rest services in web app.
You may get error like this : Could not find resource for relative : /application/test of full path: http://localhost:8080/onlinehttpclient/rest/application/test To resolve this error , you have to define resteasy.resource context param with the full path of Rest class.
Post Comments And Suggestions !! Creating Rest Services With Rest Easy In Web application

Back to Featured Articles on Logo Paperblog

Paperblog Hot Topics