The first tutorial about Jersey: Jersey 1.x - Hello, world! shows the way to develop a simple RESTful Web service application using Jersey 1.19. This tutorial shows you the way to develop the same application using Jersey 2.20. Eclipse for Java EE developers and Apache Tomcat are used to develop the application. If you do not have them, please setup them before continuing this tutorial.
For Ubuntu users:
For any users:
Integrate Tomcat with Eclipse
Step 1:
Create a new “Dynamic Web Project” in Eclipse, by New → Project → Dynamic Web Project.
Step 2:
Give the project name as “Jersey Demo” and click “Finish”, on the appeared dialog.
Step 3:
We need to create “web.xml” file in the WebContent → WEB-INF folder. It will be created automatically, if the Dynamic Web Module version is lower than 3.0; otherwise right click on the “Deployment Descriptor: Jersey Demo” and choose “Create Deployment Descriptor Stub”.
Step 4:
Using Maven, make library management easier than the pure Eclipse project. To convert this project to Maven project, right click on the project and select Configure → Convert to Maven Project.
Step 5:
Provide the group id – “com.javahelps” and artifact id – “jerseydemo” in the appeared dialog, and click on the “Finish” button.
Step 6:
Add the following dependencies to the "pom.xml" file.
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>2.20</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.20</version>
</dependency>
</dependencies>
After modification, pom.xml should look like this:
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.javahelps</groupId>
<artifactId>jerseydemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>2.20</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.20</version>
</dependency>
</dependencies>
</project>
Step 7:
Expand the src folder in the Java Resources and create a new package "com.javahelps.jerseydemo.services" inside it.
Create a new Java class “HelloWorldService” inside the package.
Step 9:
Modify the class as shown below.
package com.javahelps.jerseydemo.services;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;
@Path("/sayhello")
public class HelloWorldService {
@GET
@Path("/{name}")
public Response sayHello(@PathParam("name") String msg) {
String output = "Hello, " + msg + "!";
return Response.status(200).entity(output).build();
}
}
- @Path("/sayhello") - The URL of this service is /sayhello.
- @GET - This method is handling the Http method GET.
- @Path("/{name}") - The URL for the specific method. In this case it can be any name, which will be passed as a parameter to the method.
Create another package inside the src folder and name it as "com.javahelps.jerseydemo.app".
Step 11:
Create a new Java class "HelloWorldApplication" inside the new package and modify the class as shown below.
package com.javahelps.jerseydemo.app;
import org.glassfish.jersey.server.ResourceConfig;
public class HelloWorldApplication extends ResourceConfig {
public HelloWorldApplication() {
// Define the package which contains the service classes.
packages("com.javahelps.jerseydemo.services");
}
}
Jersey 1.x versions required to define the package which contains the Service classes in the web.xml file. According to Jersey 2.x, a subclass of ResourceConfig is used for that purpose.
Step 12:
Modify the web.xml which is available in WebContent → WEB-INF as shown below:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>Jersey Demo</display-name>
<!-- Define ServletContainer of Jersey -->
<servlet>
<servlet-name>JerseyDemo</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<!-- Define the ResourceConfig class -->
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.javahelps.jerseydemo.app.HelloWorldApplication</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Map all the URLs to the Jersey ServletContainer -->
<servlet-mapping>
<servlet-name>JerseyDemo</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
Step 12:
Save all the changes and run the project.
To run the project, right click on the project and select Run As → Run on Server.
Step 13:
Enter a URL in the following format and check the output.
../jerseydemo/sayhello/<Your-Name-Here>
For example:
../jerseydemo/sayhello/Gobinath
Find the project at Git Hub.
3 comments
Write commentsThanks! Very helpful
Replyhello,
Replyjust to help somebody who is stuck in below error.
i am was error java.lang.ClassNotFoundException: org.glassfish.jersey.servlet.ServletContainer exception while starting project .
below init param from this tutorial and checking deployment assemply in eclipse project properties helped me.
javax.ws.rs.Application
org.ssp.jaxrs.resources.JaxrsWsResourceConfig
1 more thing i want to know is can we specify package name here in web.xml 's init param itself insted of configuring package in another configuration file.
Thanks to you for this article !
ReplyEmoticonEmoticon