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
- RESTEasy 2.3.1.GA
- Tomcat 7
- 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.

To download the source code of above example, click below link.
Happy Learning !!
Steve
Hi,
Did anyone ever get this example to work ?
Thanks
Steve
Lokesh Gupta
In fact, mostly people leave comment only when they face issue. 🙂
Srikant
[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
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.
NItish
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
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.
venky
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.”
Naresh
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
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.
pavankumar
may i know how can we use bugzilla rest api methods to create bug , search bugs in bugzilla with java program automatically.
pavankumar
Please help me how to use bugzilla rest api methods with java. I want to search, create bugs in bugzilla through java program.
pavankumar
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
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
Thanks for your reply. will search the api which supports bugzilla.
confidentrakesh
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
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”.
Deepti
I am new to the webservices.Can u send any links to easily understanding webservices.
Anuj Kumar Sharma
getting this error:
Lokesh Gupta
Can you please post the POM file you are using. As far it seems resteasy version issue.
Panagiotis
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
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
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
Refer to this:.
mateen27
may i know what is the difference between RestFul (JAX-RS and JAX-B) and RestEasy framework ?
Lokesh Gupta
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