There are two main implementation of JAX-RS API.
- Jersey
- RESTEasy
In this example, we show you how to develop a simple hello world REST web application with Jersey.
Technologies and Tools used in this article:
- Jersey 1.8
- JDK 1.6
- Tomcat 7.0.42
- Maven 3.2.1
- Eclipse Kepler
Steps:
1. Download Jersey : Download the Jersey distribution as zip file from the Jersey download site. The zip contains the Jersey implementation JAR and its core dependencies. It does not provide dependencies for third party JARs beyond those for JSON support and JavaDoc.
2. Download Web container : For this example we are using Apache Tomcat. If you want to use Tomcat as servlet container please see Eclipse WTP and Apache Tomcat for instructions on how to install and use Eclipse WTP and Apache Tomcat.
3. Download and Install Maven
4. Download and Install Java
5. Create your first RESTful Webservice
5.1. Create a project with Jersey libraries
Create a new Dynamic Web Project called com.tushar.jersey.Project
Click Next
Click Next, Ensure that you create the
web.xml deployment descriptor. Click Finish.
Directory Structure :
5.2. Now convert it into maven project if you have not created it as a maven project. Right Click on the Project > Configure and click on “Convert to maven project”
Click Finish and it will convert the project into Maven Project.
5.3. Add Jersey Jar files:
Or instead of adding in POM.xml you can also:
Copy all JARs from your Jersey download into the WEB-INF/lib folder.
5.4. Create the following class.
5.5. Add the following code:
package com.tushar.jersey.Project;
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 getMsg(@PathParam("param") String msg) {
String output = "Jersey say : " + msg;
return Response.status(200).entity(output).build();
}
}
|
5.6. Define Jersey Servlet dispatcher
You need to register Jersey as the servlet dispatcher for REST requests.
In web.xml, register “com.sun.jersey.spi.container.servlet.ServletContainer“, and puts your Jersey service folder under “init-param“, “com.sun.jersey.config.property.packages“.
6. Download and Install Tomcat server
6.1. Go to Eclipse
6.2. Click Server, Right click and then click New and then click server
6.3. New screen will Pop Up, select server (selected Tomcat 7)
6.4. Click next
6.5. Select the Project and add it in the Server Configured by clicking Add button.
6.6. Click Finish, Server is added
6.7. Start the server
NOTE: In case while running the server you get the following error:
Error: The ResourceConfig instance does not contain any root resource classes.
Solution : Please remove “/” from the following line
Instead of :
@Path("/hello")
public class HelloWorld {
Remove “/” from @Path
@Path("hello")
public class HelloWorld {
7. Run your REST service
· In this example, web request from “projectURL/rest/hello/” will match to “HelloWorld“, via@Path("/hello").
· And the “{any values}” from “projectURL/rest/hello/{any values}” will match to parameter annotated with@PathParam.
URL : http://localhost:8080/ com.tushar.jersey.Project/rest/hello/tushar
NOTE: In case if you run the above URL and you are getting an issue where your Browser is showing HTTP Error 404 (but on the tab it is howing Apache Tomcat) then:
Error: HTTP Error 404
Solution : Double Click your Tomcat server, a window will open, In the window location select “Use Tomcat Installation” as selected in the below image
In case the Use Tomcat Installation is disabled then:
On the Servers view, delete all the webapps(or projects)published under your server.
· Right click on the server > Remove or right click on the server > Add and Remove and then remove manually the webapps)
· And finally right click on the server > Publish (the 'empty' content).
· Now check the screen again, This way you would enable the Server Locations area.



















No comments:
Post a Comment