HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / RESTEasy / RESTEasy example tutorial for beginners

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.

Download Source code

Happy Learning !!

Was this post helpful?

Let us know if you liked the post. That’s the only way we can improve.

Share this:

  • Twitter
  • Facebook
  • LinkedIn
  • Reddit

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Feedback, Discussion and Comments

  1. Steve

    July 15, 2019

    Hi,
    Did anyone ever get this example to work ?

    Thanks
    Steve

    • Lokesh Gupta

      July 16, 2019

      In fact, mostly people leave comment only when they face issue. 🙂

  2. Srikant

    January 11, 2015

    [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]

    • Lokesh Gupta

      January 12, 2015

      Simply write the above whole command in one line. No line breaks. we have set interactive mode to false, so it should not ask anything as input.

  3. NItish

    November 17, 2014

    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

    • Lokesh Gupta

      November 17, 2014

      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.

  4. venky

    November 7, 2014

    i copied downloaded folder in tomcat ,tomcat started
    i tried this link (http://localhost:8080/RESTfulDemoApplication/user-management/users)
    but it showing ” The requested resource (/RESTfulDemoApplication/user-management/users) is not available.”

  5. Naresh

    November 6, 2014

    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

    • Lokesh Gupta

      November 6, 2014

      Which framework you are using? For jersey, this seems easy : https://community.oracle.com/hub/

      For RESTeasy, I personally have used https://github.com/stoicflame/enunciate It is very good tool.

  6. pavankumar

    June 11, 2014

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

  7. pavankumar

    June 11, 2014

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

  8. pavankumar

    June 11, 2014

    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 .

    • Lokesh Gupta

      June 11, 2014

      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.

      • pavankumar

        June 12, 2014

        Thanks for your reply. will search the api which supports bugzilla.

  9. confidentrakesh

    April 11, 2014

    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.

    • Lokesh Gupta

      April 11, 2014

      Use this: https://docs.jboss.org/resteasy/docs/1.1.GA/userguide/html/async_job_service.html

      I will build a example for this “very soon”.

  10. Deepti

    November 22, 2013

    I am new to the webservices.Can u send any links to easily understanding webservices.

  11. Anuj Kumar Sharma

    August 4, 2013

    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]
    • Lokesh Gupta

      August 4, 2013

      Can you please post the POM file you are using. As far it seems resteasy version issue.

  12. Panagiotis

    July 22, 2013

    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?

    • Lokesh Gupta

      July 22, 2013

      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.

      • Panagiotis

        July 22, 2013

        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!!!

        • Lokesh Gupta

          July 22, 2013

          Refer to this:.

  13. mateen27

    May 9, 2013

    may i know what is the difference between RestFul (JAX-RS and JAX-B) and RestEasy framework ?

    • Lokesh Gupta

      May 9, 2013

      Hi, Its very clear from the first line:

      “RESTEasy is JBOSS provided implementation of JAX-RS specification for building RESTful Web Services and RESTful Java applications”

      REST term was originally coined by Roy Fielding :

      https://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm

Comments are closed on this article!

Search Tutorials

RESTEasy Tutorial

  • JAX-RS – Introduction
  • RESTEasy – JBoss
  • RESTEasy – Tomcat
  • RESTEasy – @Path
  • RESTEasy – HATEOAS
  • RESTEasy – SLF4J
  • RESTEasy – Log4j
  • RESTEasy – Download
  • RESTEasy – Upload (MultipartForm)
  • RESTEasy – Upload (HTTPClient)
  • RESTEasy – Custom Validation
  • RESTEasy – Hibernate Validator
  • RESTEasy – ContainerRequestFilter
  • RESTEasy – PreProcessorInterceptor
  • RESTEasy – JAXB XML
  • RESTEasy – Jettison JSON
  • RESTEasy – Jackson JSON
  • RESTEasy – ExceptionMapper

RESTEasy Client APIs

  • RESTEasy – java.net
  • RESTEasy – JAX-RS Client
  • RESTEasy – Apache HttpClient
  • RESTEasy – JavaScript API
  • RESTEasy – ResteasyClientBuilder

RESTEasy Best Practices

  • RESTEasy – Sharing Context Data
  • RESTEasy – Exception Handling
  • RESTEasy – ETag Cache control
  • RESTEasy – GZIP Compression
  • RESTful vs. SOAP

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Java 15 New Features
  • Sealed Classes and Interfaces
  • EdDSA (Ed25519 / Ed448)