Preparing for Java and Spring Boot Interview?

Join my Newsletter, its FREE

Saturday, July 8, 2023

How to Create Restful Web Services in Java using Jersey [Hello World Example]

Along with Spring Boot, Apache CXF, and  Restlet, Jersey is another popular open-source framework to create RESTful web services in Java. Jersey conforms to JAX-RS specification and actually it is the reference implementation of JAX-RS (JSR 311)( specification. In the last article, we have seen the Restlet HelloWorld Example and today we'll see the Jersey HelloWorld Example. Typically, when a developer thinks of creating a RESTful web service using Java, they assume that using a Java EE application server is the only way to create this type of application.

However, there are simpler, lightweight alternative methods for creating RESTful applications available using Java SE. This tutorial demonstrates one such alternative using the Grizzly Web server along with the Jersey REST framework. Grizzly's main use case is the web server component for the GlassFish application server.

Jersey HelloWorld Example

Here is the simple RESTful Web Service create using the Jersey REST framework. This service returns a message when a GET request is sent to the "/hello" URL. If the request also contains a request parameter then it is used in the response message.


@Path(value = "/hello")
public class JerseyHelloWorldRESTService {

  public String hello(@PathParam(value = "param") String name){
     return "Hello " + name + ", Welcome to Jersy world of RESTful web service";

That's it, you can see how simple it is to create RESTful web services using Jersey. I highly recommend you try out Jersey if you want to create a REST-based solution in Java. 

How to Create Restful Web Services in Java using Jersey [Hello World Example]

Error - No container provider supports the type class

Starting jersey grizzly ...
Jun 01, 2016 3:57:19 PM com.sun.jersey.api.core.PackagesResourceConfig init
INFO: Scanning for root resource and provider classes in the packages:

Jun 01, 2016 3:57:19 PM com.sun.jersey.api.core.ScanningResourceConfig logClasses
INFO: Root resource classes found:
class JerseyHelloWorldRESTService
Jun 01, 2016 3:57:19 PM com.sun.jersey.api.core.ScanningResourceConfig init
INFO: No provider classes found.
Exception in thread "main" java.lang.IllegalArgumentException: No container provider supports the type class org.glassfish.grizzly.http.server.HttpHandler
at com.sun.jersey.api.container.ContainerFactory.createContainer(
at com.sun.jersey.api.container.ContainerFactory.createContainer(
at com.sun.jersey.api.container.grizzly2.GrizzlyServerFactory.createHttpServer(
at JerseyServer.startServer(
at JerseyServer.main(

If you get the above error then you can add the following maven dependency (org.glassfish.jersey.containers jersey-container-jdk-http ) to solve the"):

You could change the version according to your environment. Remember the exception's error message says "No container provider supports the type interface". This dependency provides the container needed to run Jersey. 

RESTful web service using Jersey in Java

Beware of the difference between Jersey and Jersey2

It's very easy to confuse with two different versions of Jersey, in the first version the classes belong to com.sun.jersey and in the second the framework classes go to org.glassfish.jersey. Essentially only the package is different but with the various dependent JAR, it's very easy that JARs from Jersey 1.x mixed with JARs from Jersey2.x and raising dreaded NoClassDefFoundError and its close cousin ClassNotFoundException.

The package name changed because the Jersey team is now part of Glassfish. The version below 2 was using com.sun.jersey package but now since the company is different they are using org.glassfish.jersey. 

And Yes, there are some more differences in code between version 1.x and 2.x. The key takeaway doesn't mix classes and JARS from Jersey 1.x with Jersey 2.x, see here for more details on the error.

That's all about the Jersey Hello world example.  Jersey framework is more than the JAX-RS Reference Implementation. Jersey provides its own API that extends the JAX-RS toolkit with additional features and utilities to further simplify RESTful service and client development. Jersey also exposes numerous extension SPIs so that developers may extend Jersey to best suit their needs.

Other Java REST Web Service tutorials you may like
  • The difference between REST and SOAP Web Services? (answer)
  • My favorite Courses to learn REST in Java (best courses)
  • Difference between JAX-RX, Jersy, and Restlet (answer)
  • My favorite Courses to learn Spring Boto in-depth (courses)
  • Top 10 REST Web Service Interview Questions (answer)
  • 20+ Spring and REST Questions with Answers (list)
  • Spring HelloWorld Example using Dependency Injection (tutorial)
  • The difference between PUT vs POST in REST Web Service? (article)
  • How to create a JDBC connection pool using Spring? (tutorial)
  • How to parse large JSON responses using Jackson? (tutorial)
  • 20 Hibernate Interview Questions for Java developers (article)
  • The difference between Idempotent and safe methods in HTTP? (answer)
  • How to convert JSON array to String array in Java? (tutorial)
  • 5 Best Courses to learn Microservices in Java (Online courses)

No comments :

Post a Comment