RESTEasy is one of the implementation of JAX-RS specification provided by JBOSS for building RESTful Web Services. In this tutorial we will create a simple hello world web service with the JAX-RS reference implementation RESTEasy.
Folder Structure :
- Create a Dynamic Web Project RestfulExample and create a package for our src files “com.javainterviewpoint“
- Place the required jar files under WEB-INF/Lib
resteasy-jaxrs-3.0.4.Final.jar
jaxrs-api-3.0.4.Final.jar
javassist-3.20.0-GA.jar
scannotation-1.0.2.jar
jboss-logging-3.3.0.Final.jar - Create the Java classes HelloWorld_RESTEasy.java under com.javainterviewpoint folder.
- Place the web.xml under the WEB-INF directory
HelloWorld_RESTEasy.java
package com.javainterviewpoint; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.core.Response; @Path("/hello") public class HelloWorld_RESTEasy { @GET @Path("/sayHello/{user}") public Response dispMessage(@PathParam("user") String msg) { String message = "Welcome " + msg+"!!!"; return Response.status(200).entity(message).build(); } }
- We have annotated @Path(“/hello”) at the class level, which means our RESTEasy service can be reached through the URL …/hello
- All the GET Request will be handled by the dispMessage() method, which we have annotated with @GET
- @Path(“sayHello/{user}”) will hold the value which will be passed in the URL after ../hello/sayHello/XXXX, Using the @PathParam we will get the value of the “user” and will be assigned to String msg.
- Finally we are appending the “user” with Welcome string and send our Response back.
web.xml
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>RESTEasy Restful Web Application</display-name> <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>/rest/*</url-pattern> </servlet-mapping> <!-- Auto scanning REST service --> <context-param> <param-name>resteasy.scan</param-name> <param-value>true</param-value> </context-param> <!-- Prefix need to be set if url pattern is not /* --> <context-param> <param-name>resteasy.servlet.mapping.prefix</param-name> <param-value>/rest</param-value> </context-param> <listener> <listener-class> org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap </listener-class> </listener> </web-app>
- In our “web.xml” file, we have registered “org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher” as our servlet container
- We have also registered a listener “org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap”.
- The ResteasyBootstrap listener is responsible for initializing the basic components of RESTeasy as well as scanning for annotation classes. It also reads the configuration options from <context-param> elements of the “web.xml” file.
- The configuration prefix must be set if the url-pattern of the <servlet-mapping> is not “/*”. This will be taken care by “resteasy.servlet.mapping.prefix”, the value provided should same as the url-pattern of the servlet-mapping except “/*”. In our case we have url-pattern as “/rest/*” and hence the “resteasy.servlet.mapping.prefix” value should be “/rest”
- One more configuration is added “resteasy.scan”, when this values is set to “true”, this will tell ResteasyBootstrap to automatically search for REST Services Implementation, both @Provider and JAX-RS resource classes (@Path, @GET, @POST etc…) and register them.
Lets run our application
Now are good to run our application, add the project to the server. I use Tomcat as my server you are free to choose your own.
http://localhost:8080/RestfulExample/rest/hello/sayHello/<<parameter passed>>
The request url has to match http://<<server location>>:<<port>>/<<url-pattern if any>>/<<Path mentioned in java @Path>>/{any value}
Once i hit the url in POSTMAN client
Alternate way of Registering RESTEasy WebService
In the above code we have registerd our register REST service via ResteasyBootstrap listener. Here is another way of registering the RESTEasy WebService manually. We need to create class and extends “javax.ws.rs.core.Application” and add our REST service manually.
package com.javainterviewpoint; import java.util.HashSet; import java.util.Set; import javax.ws.rs.core.Application; public class RestApplication extends Application { private Set<Object> singletons = new HashSet<Object>(); public RestApplication() { singletons.add(new HelloWorld_RESTEasy()); } @Override public Set<Object> getSingletons() { return singletons; } }
We have defined our Application class as a Set that which will hold all the root resource and provider classes.
We need to modifiy our web.xml accordingly.
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee%20 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>RESTEasy Restful Web Application</display-name> <servlet> <servlet-name>resteasy-servlet</servlet-name> <servlet-class> org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher </servlet-class> <init-param> <param-name>javax.ws.rs.Application</param-name> <param-value>com.javainterviewpoint.RestApplication</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>resteasy-servlet</servlet-name> <url-pattern>/rest/*</url-pattern> </servlet-mapping> <!-- Prefix need to be set if url pattern is not /* --> <context-param> <param-name>resteasy.servlet.mapping.prefix</param-name> <param-value>/rest</param-value> </context-param> </web-app>
In the web.xml we do not need to add a listener or enable the auto scan service feature as we have defined our own application implementation as a parameter in the servlet definition(init-param).
Leave a Reply