In this tutorial, we will create a simple hello world web service with the JAX-RS reference implementation Jersey, which is the reference implementation of the JSR 311 specification.JAX-RS is part of the Java EE. REST (REpresentational State Transfer) is a simple stateless architecture that generally runs over HTTP. Now let’s see create our first JAX-RS service
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
jersey-bundle-1.18.jar
asm-3.1.jar - Create the Java classes HelloWorld.java under com.javainterviewpoint folder.
- Place the web.xml under the WEB-INF directory
HelloWorld.java
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 { @GET @Path("/{param}") public Response dispMessage(@PathParam("param")String msg ) { String output = "Welcome to RESTful Jersey example - "+msg; return Response.status(200).entity(output).build(); } }
- We have annotated @Path(“/hello”) at the class level, which means our HelloWorld 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(“/{param}”) will hold the value which will be passed in the URL after ../hello/XXXX, Using the @PathParam we will get the value of the “param” and will be assigned to String msg.
- Finally, we will send our Response back.
web.xml
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:web="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>Restful Web Application</display-name> <servlet> <servlet-name>jersey-serlvet</servlet-name> <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class> <init-param> <param-name>com.sun.jersey.config.property.packages</param-name> <param-value>com.javainterviewpoint</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>jersey-serlvet</servlet-name> <url-pattern>/rest/*</url-pattern> </servlet-mapping> </web-app>
In web.xml, we have to register “com.sun.jersey.spi.container.servlet.ServletContainer“, and mention your source folder under init-param, “com.sun.jersey.config.property.packages“ so that Jersey will look for the web service classes under the package mentioned.
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/<<parameter passed>>
The request url has to match http://<<server location>>:<<port>>/<<url-pattern if any>>/<<Path mentioned in java @Path>>/{any value}
Leave a Reply