Search This Blog

Jersey JAX-RS Restful CRUD Web Services Example

This is step by step guide to developing a CRUD Rest API using Jersey JAX-RS.

In this guide, 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).Technology and tools used:

Maven 3

JDK 8

Jersey 2.27

Apache Tomcat 8.5

Eclipse Neon

1. Create Maven Web Application Project

You can create a quick start Java web application project by using the Maven maven-archetype-webapp template. In a terminal (*Unix or Mac) or command prompt (Windows), navigate to the folder you want to create the project.

6. 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:

importjavax.ws.rs.ApplicationPath;
// Deployment of a JAX-RS application using @ApplicationPath with Servlet 3.0// Descriptor-less deploymentimportorg.glassfish.jersey.server.ResourceConfig;
@ApplicationPath("/api")
publicclassJerseyServletContainerConfigextendsResourceConfig {
publicJerseyServletContainerConfig() {
// if there are more than two packanges then separate them with semicolon// exmaple : packages("org.foo.rest;org.bar.rest");
packages("com.javadevelopersguide.jersey.resources");
}
}

7. Create Sample UserService class

Created UserService class to serve in-memory objects to UserResource class.

8. Jersey JAX-RS Client for Rest API

Jersey Test Framework originated as an internal tool used for verifying the correct implementation of server-side components.

If you want to develop a test using Jersey Test Framework, you need to subclass JerseyTest and configure the set of resources and/or providers that will be deployed as part of the test application.

The below short code snippet shows basic resource class UserResource used in tests defined as part of the UserResourceTest class. The overridden configure method returns a ResourceConfig of the test application, that contains only the UserResource resource class. ResourceConfig is a sub-class of JAX-RS Application. It is a Jersey convenience class for configuring JAX-RS applications. ResourceConfig also implements JAX-RS Configurable interface to make the application configuration more flexible.

10. Conclusion

This guide illustrated how to implement a Jersey JAX-RS Restful CRUD Web Services Example using latest jersey rest 2.27, jersey client and ResourceConfig @ApplicationPath annotation based configuration.In the next guide of the series, we will focus on more Jersey rest examples, concepts, and more.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.Github Repository: Jersey JAX-RS Restful CRUD Web Services Example