RESTEasy example tutorial for beginners

RESTEasy is JBOSS provided implementation of JAX-RS specification for building REST APIs and RESTful Java applications. Though RESTEasy is not limited to be used in JBOSS only, and we can use with other servers also. In this RESTEasy example, learn to create restful webservices in Java using eclipse and tomcat

1. Development Environment

  1. RESTEasy 2.3.1.GA
  2. Tomcat 7
  3. JDK 1.6

Follow below steps to build a demo application.

2. Create maven eclipse web project

Run these commands to create a maven project and convery to eclipse project.

$ mvn archetype:generate -DgroupId=com.howtodoinjava -DartifactId=RESTfulDemoApplication 
-DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false

$ mvn eclipse:eclipse -Dwtpversion=2.0

3. RESTEasy maven dependencies

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.howtodoinjava</groupId>
  <artifactId>RESTfulDemoApplication</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>RESTfulDemoApplication Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <repositories>
   	<repository>
      <id>jboss</id>
      <url>http://repository.jboss.org/maven2</url>
   	</repository>
  </repositories>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <!-- core library -->
	<dependency>
		<groupId>org.jboss.resteasy</groupId>
		 <artifactId>resteasy-jaxrs</artifactId>
		<version>2.3.1.GA</version>
	</dependency>
	<dependency>
		<groupId>net.sf.scannotation</groupId>
		<artifactId>scannotation</artifactId>
		<version>1.0.2</version>
	</dependency>
  </dependencies>
  <build>
    <finalName>RESTfulDemoApplication</finalName>
  </build>
</project>

4. Register HttpServletDispatcher

RESTeasy is implemented as a Servlet and deployed within a WAR file. HttpServletDispatcher class is responsible for initializing some basic components of RESTeasy.

resteasy.scan property automatically scan WEB-INF/lib jars and WEB-INF/classes directory for both @Provider and JAX-RS resource classes (@Path, @GET, @POST etc..) and register them.

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  	<display-name>Archetype Created Web Application</display-name>
  	<!-- Auto scan REST service -->
	<context-param>
		<param-name>resteasy.scan</param-name>
		<param-value>true</param-value>
	</context-param>
	
	<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>/*</url-pattern>
	</servlet-mapping>
</web-app>

5. Create REST Controller

package com.howtodoinjava.restful;

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

@Path("/user-management")
public class UserManagementModule
{
	@GET
	@Path("/users")
	public Response getAllUsers()
	{
		String result = "<h1>RESTful Demo Application</h1>In real world application, a collection of users will be returned !!";
		return Response.status(200).entity(result).build();
	}
}

6. RESTEasy example application demo

When we deploy above built application in tomcat and hit the URL: ” http://localhost:8080/RESTfulDemoApplication/user-management/users”, below is the response.

JAX-RS + Tomcat example
JAX-RS + Tomcat example

To download the source code of above example, click below link.

Happy Learning !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

25 thoughts on “RESTEasy example tutorial for beginners”

  1. [Hi Lokesh,

    Iam new to webservice, when I run the “mvn archetype:generate -DgroupId=com.howtodoinjava -DartifactId=RESTfulDemoApplication
    -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false” command it is downloading required jars and after sometime it asking to enter “Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): 513: -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
    Choose archetype:” what i will enter for that? Please help me and suggest me if it will ask for further options next]

    Reply
  2. HI Lokesh,
    will it make any difference in running 10 different war files in single tomcat vs multiple instances of tomcat (only one catalina_home and 10 different catalinabase)

    is there any pros & cons in running multiple instances of tomcat? will it help in performance? or etc

    Reply
    • I don’t think that any performance gain will be there. A good benefit I see is instance specific configuration ability. This configuration can be related to application runtime configuration as well as security settings such as different access rights to specific folders for different application users.

      Reply
  3. Hi Lokesh,

    I have developed a sample RestFul java application with Spring.I want generate WADL file for that project.How? My IDE is RAD.
    Please help me out.
    Thanks

    Reply
  4. may i know how can we use bugzilla rest api methods to create bug , search bugs in bugzilla with java program automatically.

    Reply
  5. Please help me how to use bugzilla rest api methods with java. I want to search, create bugs in bugzilla through java program.

    Reply
  6. HI Lokesh, can you help me how to use REST API methods of bugzilla with Java. I am newer to Webservices so Please advise me which tool do i need to use JSON or Apache Client .

    Reply
    • Currently I do not have clear understanding of client APIs of Bugzilla. I will try to find time to do some research on this. My advice: try to find some already existing client API supporting request-response to bugzilla. Don’t write everything from scratch.

      Reply
  7. HI Lokesh,

    I have a scenario where I need to develop a restful service using tomcat and jersey. Following is what needs to be done.
    It a post service which I have created, now when a request comes in I need to send one response immediately/synchronously and send back a second response asynchronously. Could you please let me know how to achieve it.

    Reply
  8. getting this error:

    22:22:12,279 ERROR [org.jboss.remoting.remote.connection] (Remoting "vostro:MANAGEMENT" read-1) JBREM000200: Remote connection failed: java.io.IOException: An existing connection w
    as forcibly closed by the remote host
    22:22:41,585 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/RESTfulDemoApplication]] (http--127.0.0.1-8080-1) StandardWrapper.Throwable: java.lang.Runti
    meException: Unable to find a public constructor for class org.jboss.resteasy.core.AsynchronousDispatcher
            at org.jboss.resteasy.plugins.server.resourcefactory.POJOResourceFactory.registered(POJOResourceFactory.java:35) [resteasy-jaxrs-2.3.2.Final.jar:]
            at org.jboss.resteasy.core.ResourceMethodRegistry.addResourceFactory(ResourceMethodRegistry.java:121) [resteasy-jaxrs-2.3.2.Final.jar:]
            at org.jboss.resteasy.core.ResourceMethodRegistry.addResourceFactory(ResourceMethodRegistry.java:107) [resteasy-jaxrs-2.3.2.Final.jar:]
            at org.jboss.resteasy.core.ResourceMethodRegistry.addResourceFactory(ResourceMethodRegistry.java:84) [resteasy-jaxrs-2.3.2.Final.jar:]
            at org.jboss.resteasy.core.ResourceMethodRegistry.addPerRequestResource(ResourceMethodRegistry.java:73) [resteasy-jaxrs-2.3.2.Final.jar:]
            at org.jboss.resteasy.spi.ResteasyDeployment.registration(ResteasyDeployment.java:367) [resteasy-jaxrs-2.3.2.Final.jar:]
            at org.jboss.resteasy.spi.ResteasyDeployment.start(ResteasyDeployment.java:225) [resteasy-jaxrs-2.3.2.Final.jar:]
            at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.init(ServletContainerDispatcher.java:67) [resteasy-jaxrs-2.3.2.Final.jar:]
            at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.init(HttpServletDispatcher.java:36) [resteasy-jaxrs-2.3.2.Final.jar:]
            at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1202) [jbossweb-7.0.13.Final.jar:]
            at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:952) [jbossweb-7.0.13.Final.jar:]
            at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:188) [jbossweb-7.0.13.Final.jar:]
            at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161) [jbossweb-7.0.13.Final.jar:]
            at org.jboss.modcluster.container.jbossweb.JBossWebContext$RequestListenerValve.event(JBossWebContext.java:67)
            at org.jboss.modcluster.container.jbossweb.JBossWebContext$RequestListenerValve.invoke(JBossWebContext.java:48)
            at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:153) [jboss-as-web-7.1.1.Final.jar:7.1.1.Final]
            at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155) [jbossweb-7.0.13.Final.jar:]
            at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [jbossweb-7.0.13.Final.jar:]
            at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [jbossweb-7.0.13.Final.jar:]
            at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368) [jbossweb-7.0.13.Final.jar:]
            at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877) [jbossweb-7.0.13.Final.jar:]
            at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:671) [jbossweb-7.0.13.Final.jar:]
            at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:930) [jbossweb-7.0.13.Final.jar:]
            at java.lang.Thread.run(Thread.java:662) [rt.jar:1.6.0_45]
    
    22:22:41,626 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/RESTfulDemoApplication].[resteasy-servlet]] (http--127.0.0.1-8080-1) Allocate exception for
    servlet resteasy-servlet: java.lang.RuntimeException: Unable to find a public constructor for class org.jboss.resteasy.core.AsynchronousDispatcher
            at org.jboss.resteasy.plugins.server.resourcefactory.POJOResourceFactory.registered(POJOResourceFactory.java:35) [resteasy-jaxrs-2.3.2.Final.jar:]
            at org.jboss.resteasy.core.ResourceMethodRegistry.addResourceFactory(ResourceMethodRegistry.java:121) [resteasy-jaxrs-2.3.2.Final.jar:]
            at org.jboss.resteasy.core.ResourceMethodRegistry.addResourceFactory(ResourceMethodRegistry.java:107) [resteasy-jaxrs-2.3.2.Final.jar:]
            at org.jboss.resteasy.core.ResourceMethodRegistry.addResourceFactory(ResourceMethodRegistry.java:84) [resteasy-jaxrs-2.3.2.Final.jar:]
            at org.jboss.resteasy.core.ResourceMethodRegistry.addPerRequestResource(ResourceMethodRegistry.java:73) [resteasy-jaxrs-2.3.2.Final.jar:]
            at org.jboss.resteasy.spi.ResteasyDeployment.registration(ResteasyDeployment.java:367) [resteasy-jaxrs-2.3.2.Final.jar:]
            at org.jboss.resteasy.spi.ResteasyDeployment.start(ResteasyDeployment.java:225) [resteasy-jaxrs-2.3.2.Final.jar:]
            at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.init(ServletContainerDispatcher.java:67) [resteasy-jaxrs-2.3.2.Final.jar:]
            at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.init(HttpServletDispatcher.java:36) [resteasy-jaxrs-2.3.2.Final.jar:]
            at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1202) [jbossweb-7.0.13.Final.jar:]
            at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:952) [jbossweb-7.0.13.Final.jar:]
            at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:188) [jbossweb-7.0.13.Final.jar:]
            at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161) [jbossweb-7.0.13.Final.jar:]
            at org.jboss.modcluster.container.jbossweb.JBossWebContext$RequestListenerValve.event(JBossWebContext.java:67)
            at org.jboss.modcluster.container.jbossweb.JBossWebContext$RequestListenerValve.invoke(JBossWebContext.java:48)
            at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:153) [jboss-as-web-7.1.1.Final.jar:7.1.1.Final]
            at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155) [jbossweb-7.0.13.Final.jar:]
            at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [jbossweb-7.0.13.Final.jar:]
            at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [jbossweb-7.0.13.Final.jar:]
            at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368) [jbossweb-7.0.13.Final.jar:]
            at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877) [jbossweb-7.0.13.Final.jar:]
            at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:671) [jbossweb-7.0.13.Final.jar:]
            at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:930) [jbossweb-7.0.13.Final.jar:]
            at java.lang.Thread.run(Thread.java:662) [rt.jar:1.6.0_45]
    Reply
  9. I’m trying to get this to work in wildfly-8.0.0.Alpha3 but without success. I notice in the JBoss tutorial (which is deployed perfectly in wildfly but not in tomcat) that there are some differences. The web.xml is quite small there and there is a registration of the application path as well. I tried both but without success. Can you point me to the right direction? Why isn’t there a hello world for both servers?

    Reply
    • Jboss has inbuilt support for resteasy tht’ why you don’t need to specify anything extra in web.xml while tomcat demands external configuration.

      Can you please look at logs and see if there is any error on tomcat server logs? Paste that here.

      Reply
      • there is nothing wrong in the tomcat logs, the tutorial works great for tomcat. What I’m trying to do is make it work for wildfly (jboss) as well.

        I added an ApplicationConfig class, and I emptied the web.xml file and now it works!

        Do you have any idea why this part causes a problem for jboss?

        resteasy-servlet

        org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher

        Thans a lot!!!

        Reply

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.