In this example, we will learn how to develop a simple Jersey Rest hello world example.
JAX-RS Specification is the Java API for RESTful web services. Jersey is the open source reference implementation of Java JAX-RS specification. It provides a Java library using which we can easily create RESTful web services in Java platform. JAX-RS / Jersey supports JAXB based XML bindings as well.
Technologies and tools used
- Jersey 2.27
- JDK 1.8
- Tomcat 8.5
- Maven 3.0.3
- Eclipse Neon
In this tutorial, we have used the latest Jersey version 2.27 with JDK 8. Deployment of a JAX-RS application using @ApplicationPath with Servlet 3.0 ( use @ApplicationPath("resources") annotation to configure Jersey Servlet Container).
Development Steps
- Create Maven Web Application Project
- Project Packaging Structure
- Update Jersey Dependencies in a Pom.Xml File
- Create Resource - HelloWorldResource.java
- Descriptor-Less Deployment Configuration
- Build and Deployment
- Conclusion
1. Create a Maven Web Application Project
Use How to Create a Web Project Using Maven in Eclipse - In this article, we will show you how to create a web project or application using maven in Eclipse IDE.
2. Project Packaging Structure
In a typical Jersey application, the project packaging structure may look:
3. Update Jersey Dependencies in a Pom.Xml File
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.javadevelopersguide.jersey</groupId>
<artifactId>jersey-helloworld-example</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>jersey-helloworld-example Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.7</version>
</dependency>
</dependencies>
<build>
<finalName>jersey-helloworld-example</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
</plugin>
</plugins>
</build>
</project>
4. Create Resource - HelloWorldResource.java
package com.javadevelopersguide.jersey.helloworld;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
@Path("/")
public class HelloWorldResource {
@GET
@Path("helloworld")
public String helloworld() {
return "Helloworld !!!!";
}
}
5. Descriptor-Less Deployment Configuration
Here are multiple deployment options in the Servlet 3.0 container for a JAX-RS application defined by implementing a custom Application subclass. For simple deployments, no web.xml is necessary at all.
Instead, an @ApplicationPath annotation can be used to annotate the custom Application subclass and define the base application URI for all JAX-RS resources configured in the application:
package com.javadevelopersguide.jersey.config;
import javax.ws.rs.ApplicationPath;
import org.glassfish.jersey.server.ResourceConfig;
@ApplicationPath("resources")
public class MyApplication extends ResourceConfig {
public JerseyServletContainerConfig() {
// if there are more than two packanges then separate them with semicolon
// exmaple : packages("org.foo.rest;org.bar.rest");
packages("com.javadevelopersguide.jersey.helloworld");
}
}
6. Build and Deployment
Let's build this maven project using the following maven command:
mvn clean install
Or, you can build from Eclipse IDE - Right click on a project -> Run As -> Maven Build and enter below command in the wizard:
clean install
Once, build successful, then deploy this project in apache tomcat server 8.5. Once the application is up and running, type http://localhost:8080/jersey-helloworld-example/ link in a browser will show below output:
7. Conclusion
This guide illustrated how to implement a simple hello world example using the latest jersey rest 2.27 and ResourceConfig @ApplicationPath annotation based configuration.
All the code of this article is available over on Github. This is a Maven-based project, so it should be easy to import and run as it is.
Comments
Post a Comment
Leave Comment