Jersey Web Service Hello World Example in Java
After Restlet, Jersey is another popular open source framework to create RESTful web services in Java. Jersey conforms 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 web server component for the GlassFish application server.
Jersey HelloWorld Example
Here is the simple RESTful Web Service create using 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"; } }
Error
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 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 the
Jersey.
Beware of difference between Jersey and Jersey2
It’s very easy to confuse with two different version of Jersey, in the first version the classes belongs to com.sun.jersey and in second the framework classes goes to org.glassfish.jersey. Essentially only the package is different but with various dependent JAR, it’s very easy that JARs from Jersey 1.x mixed with JARs from Jersey2.x and raising dreaded
NoClassDefFoundError and it’s close cousin ClassNotFoundException.
The package name changed because 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. 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 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)
- Top 10 REST Web Service Interview Questions (answer)
- Spring HelloWorld Example using Dependency Injection (tutorial)
- The difference between PUT vs POST in REST Web Service? (article)
- How to create JDBC connection pool using Spring? (tutorial)
- How to parse large JSON response 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)
Reference
P.S. – If you want to learn more about developing RESTful web services in Java using Jersey or Restlet, I suggest you to first read a good book on JAX-RS, which is the standard behind these frameworks e.g.
RESTful Java with JAX-RS 2.0, a good book to learn to design and developing distributed web services.
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.
Reference: | Jersey Web Service Hello World Example in Java from our JCG partner Javin Paul at the Javarevisited blog. |