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)(http://jsr311.java.net/nonav/releases/1.1/index.html) 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 webserver component for the GlassFish application server.

And, if you are a complete beginner into RESTful web services then I also suggest you take a look at the Master Java Web Service and RESTful API with Spring Boot course by Ranga Rao on Udemy. It's a great hands-on course to learn how to develop RESTful web service in Java.




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.

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;

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

  @GET
  @Path(value="/{param}")
  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. If you want to learn more, you can also checkout RESTFul Services in Java using Jersey By Bryan Hansen on Pluralsight, one of the better courses to learn Restful Web services using Jersey for Java developers. 




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(ContainerFactory.java:196)
at com.sun.jersey.api.container.ContainerFactory.createContainer(ContainerFactory.java:134)
at com.sun.jersey.api.container.grizzly2.GrizzlyServerFactory.createHttpServer(GrizzlyServerFactory.java:242)
at JerseyServer.startServer(JerseyServer.java:24)
at JerseyServer.main(JerseyServer.java:28)


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 problem:com.sun.net.httpserver.HttpHandler"):
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-jdk-http</artifactId>
        <version>2.18</version>
    </dependency>

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. You can also see the Java Web Services course by Bharat Thippireddy on Udemy to learn more about building and deploying web services in Java. 

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.

Further Learning
REST Java Web Services
REST API Design, Development & Management
RESTFul Services in Java using Jersey By Bryan Hansen


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)

P. S. - If you want to learn how to develop RESTful Web Services using Spring Framework, check out Eugen Paraschiv's REST with Spring course. He has recently launched the certification version of the course, which is full of exercises and examples to further cement the real-world concepts you will learn from the course.

No comments :

Post a Comment