Getting started with JBoss RESTEasy 1.0
So ready to jump start into JBoss RESTEasy? Dive!
Writing the JAX-RS Class
The first step, let us write a simple echo service using JAX-RS:
package testpackage;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;
@Path("/echo")
public class Echo {
@GET
@Path("/{message}")
public Response echoService(@PathParam("message") String message){
return Response.status(200).entity(message).build();
}
}
Adding the JBoss RESTEasy dependency to Maven project
If you are using Maven to build your projects, add the dependency for RESTEasy:
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>1.0.0.GA</version>
</dependency>
Also remember to add the JBoss repository to the pom.xml (RESTEasy is not available in Maven public repository):
<repository>
<id>jboss</id>
<url>http://repository.jboss.com/maven2/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
Configuring web.xml
The bare minimum needed to have RESTEasy running is:
<web-app ...>
...
<listener>
<listener-class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class>
</listener>
<servlet>
<servlet-name>Resteasy</servlet-name>
<servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Resteasy</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
...
</web-app>
Above, we have just added the RESTEasy servlet and listener. Now we have to make it load our JAX-RS class. This can be done in two ways. One, just let RESTEasy scan the CLASSPATH to identify and load JAX-RS resources:
<web-app ...>
...
<context-param>
<param-name>resteasy.scan</param-name>
<param-value>true</param-value>
</context-param>
...
</web-app>
The other option is to specify the resources manually (use CSV format):
<web-app ...>
...
<context-param>
<param-name>resteasy.resources</param-name>
<param-value>testpackage.Echo</param-value>
</context-param>
...
</web-app>
After deploying this, you may access your application (if your are using default Tomcat/Jetty installation): http://localhost:8080/resteasytest/echo/hello,world (assumes your application is deployed as resteasytest.war)
Thanks for the post. It helped a lot. I tried it with 1.0.2GA and Tomcat 6, had to add folllowing dependency to make it work.
org.jboss.resteasy
resteasy-multipart-provider
1.0.2.GA
su
17 May 09 at 4:29 am
@su thanks for informing about the dependency
Subhash Chandran
10 Jun 09 at 3:49 pm
@su I wish I had read your comment about an hour ago
Chinmay Garde
27 Jan 10 at 6:51 pm
@subash This is really a great way to get your hands dirty with RESTEasy. I am novice to maven as well as RESTEasy and it made really my life difficult in sorting out some steps. Anyways, I have made this working with RESTEasy 1.2.GA and JBoss 6.0.x.
Thanks for the wonderful tutorial.
Muralidhar Chapa
15 Jul 10 at 3:12 am
Great tutorial. It really helped me to get my first RESTEasy service up and running.
Daniel Gorst
22 Aug 10 at 6:38 pm
Yes, on JBoss-6 you need to upgrade the resteasy-jaxrs version.
Otherwise you get exception:
… ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[localhost].[/myapp]]
Exception sending context initialized event to listener instance of class org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap:
java.lang.ClassCastException: org.jboss.resteasy.plugins.providers.jaxb.JAXBXmlRootElementProvider cannot be cast to javax.ws.rs.ext.MessageBodyReader
This version works fine:
org.jboss.resteasy
resteasy-jaxrs
2.1.0.GA
rop
13 Apr 11 at 3:31 am
Hi Subhash, thanks for the post! Just a quick update on the dependencies:
…
org.jboss.resteasy
resteasy-jaxrs
2.2.0.GA
…
JBoss Releases
https://repository.jboss.org/nexus/content/repositories/releases/
JBoss Thirdparty Releases
https://repository.jboss.org/nexus/content/repositories/thirdparty-releases/
Nils
23 Jun 11 at 7:50 pm
Hi Subhash, thanks for the post! Just a quick update on the dependencies:
<!– RESTEasy –><dependency> <groupId>org.jboss.resteasy</groupId> <artifactId>resteasy-jaxrs</artifactId> <version>2.2.0.GA</version></dependency>…<repositories> <repository> <id>JBoss Releases</id> <url>https://repository.jboss.org/nexus/content/repositories/releases/</url> </repository> <repository> <id>JBoss Thirdparty Releases</id> <url>https://repository.jboss.org/nexus/content/repositories/thirdparty-releases/</url> </repository></repositories>
Cheers,
Nils
Nils
23 Jun 11 at 7:53 pm
Hej,
thanks for the post. I tried it with JBoss AS 7.1 and Resteasy 2.3.3.Final. resteasy.scan doesn’t work as expected. I got a 404. Specifying resource like described works.
Yours,
Matthias.
Matthias
4 May 12 at 5:35 pm