Introduction to JAX-RS Jersey
Sun’s production-quality reference version of JAX-RS, or the Java API for RESTful Web Services, is called Jersey. The Java Jersey project keeps track of the JAX-RS API, which Java programmers use to build Restful web services. A JAX-RS implementation is called Jersey. The JAX-RS Reference Implementation is only one component of the Jersey framework. Jersey offers its API that adds additional features and tools to the JAX-RS toolset to make developing RESTful services and clients easier. Developers can easily create RESTful web services using the Java framework and Java JVM thanks to Jersey’s implementation of support for the annotations provided in JSR-311.
Key Takeaways
- Easy to create web services API for simple xml and HTML output.
- Simple and understandable installation method with or without Maven.
- Open source framework for the development of complicated applications.
- High-quality application with stability and security.
What is JAX-RS Jersey Framework?
The Jersey RESTful Services framework is used to create Java web services applications that use JAX-RS APIs. An open-source framework for building Web Services is called Jersey. Sun Microsystem first offered the JAX-RS Jersey framework as a referenced, implemented, and production-quality tool for Java applications. The Jersey framework supports using ATOM syndication feeds, JSON as a wire format for requests, and XML serialization of JAXB beans and response entities. It is integrated with JavaMail, the Spring container, and the Activation Java Framework. Although it is stored in a Maven repository, maven is not necessary to use it.
How to Install JAX-RS Jersey?
Given below shows the method of installation:
Method 1
Compile below Gradle
‘org.glassfish.jersey.containers:jersey-container-servlet:2.25.1’
Method 2
Adding Jersey libraries manually to an Eclipse project.
The Jersey download site downloads the Jersey distribution as a zip file.
The Jersey implementation JAR and its primary dependencies are contained in the zip. Beyond those for JSON support and JavaDoc, it does not provide dependencies for third-party JARs.
Step 1: You may download Jersey and maintain it current if you use Maven. The Jersey starting page includes links to the POM files. You specifically require the grizzly-servlet-webserver module and the jersey-server module.
Only a few JARs are required if you are not using Maven:
- grizzly-servlet-webserver.jar
- jersey-server.jar
- jersey-core.jar
- jsr311-api.jar
- asm.jar
Once you have the JARs, set up your classpath or IDE to include them in your project. You may also want the source and Javadoc jars to go with each JAR. After that, you are prepared to write the required “Hello world” program.
RESTful Web Services
Given below are the RESTful web services:
Step 1: Configure Eclipse WTP and jersey use in a new Gradle project.
- Set up Eclipse WTP and create a new Gradle project with “com.educba.jersey.demo” as the top-level package name. Refer to the Required setup for Gradle and Eclipse web projects to get started.
- Add the subsequent dependency to your build to import the Jersey dependencies.
compile ‘org.glassfish.jersey.containers:jersey-container-servlet:2.25.1’
Step 2: Use the following file format of the Java program.
Code:
package com.educba.jersey.demo;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/start")
public class Start {
@GET
@Produces(MediaType.TEXT_PLAIN)
public String sayPlainTextStart() {
return "Start Jersey";
}
@GET
@Produces(MediaType.TEXT_HTML)
public String sayHtmlStart() {
return "<html> " + "<title>" + "Start Jersey" + "</title>"
+ "<body><h1>" + "Start Jersey" + "</body></h1>" + "</html> ";
}
}
Step 3: Use the Jersey Servlet dispatcher in the web file.
Open the web.xml file and use the following elements for the JAX-RS Jersey framework.
Code:
<?xml version = "1.0" encoding = "UTF-8"?>
<web-app xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xmlns = "http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id = "Web_ID" version = "3.0">
<display-name> com.educba.jersey.demo </display-name>
<servlet>
<servlet-name>
Jersey Service
</servlet-name>
<servlet-class>
org.glassfish.jersey.servlet.ServletContainer
</servlet-class>
<init-param>
<param-name>
jersey.config.server.provider.packages
</param-name>
<param-value> com.educba.jersey.demo </param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>
Jersey Service
</servlet-name>
<url-pattern>
/codes/*
</url-pattern>
</servlet-mapping>
</web-app>
Example
For the JAX-RS jersey basic example, four files have been created:
- Start.java
- web.xml
- index.html
- StartClient.java
Start.java:
Code:
package com.educba.jersey.demo;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/start")
public class Start {
@GET
@Produces(MediaType.TEXT_PLAIN)
public String sayPlainTextStart() {
return "Start Jersey";
}
@GET
@Produces(MediaType.TEXT_XML)
public String sayXMLHello() {
return "<?xml version=\"1.0\"?>" + "<start> Start Jersey" + "</start>";
}
@GET
@Produces(MediaType.TEXT_HTML)
public String sayHtmlStart() {
return "<html> " + "<title>" + "Start Jersey" + "</title>"
+ "<body><h1>" + "Start Jersey" + "<h1></body>" + "</html> ";
}
}
Web.xml:
Code:
<?xml version = "1.0" encoding = "UTF-8"?>
<web-app xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xmlns = "http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id = "Web_ID" version = "3.0">
<display-name> com.educba.jersey.demo </display-name>
<servlet>
<servlet-name>
Jersey Service
</servlet-name>
<servlet-class>
org.glassfish.jersey.servlet.ServletContainer
</servlet-class>
<init-param>
<param-name>
jersey.config.server.provider.packages
</param-name>
<param-value> com.educba.jersey.demo </param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>
Jersey Service
</servlet-name>
<url-pattern>
/codes/*
</url-pattern>
</servlet-mapping>
</web-app>
index.html:
Code:
<a href = "codes/start"> Click on the link </a>
StartClient.java:
Code:
package com.educba.jersey.demo;
import java.net.URI;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriBuilder;
import org.glassfish.jersey.client.ClientConfig;
public class StartClient {
public static void main(String[] args) {
ClientConfig configdata = new ClientConfig();
Client clientdata = ClientBuilder.newClient(configdata);
WebTarget targetdata = clientdata.target(getBaseURI());
System.out.println(targetdata.path("codes").path("start").request().accept(MediaType.TEXT_PLAIN).get(String.class));
System.out.println(targetdata.path("codes").path("start").request().accept(MediaType.TEXT_XML).get(String.class));
System.out.println(targetdata.path("codes").path("start").request().accept(MediaType.TEXT_HTML).get(String.class));
}
private static URI getBaseURI() {
return UriBuilder.fromUri("http://localhost:4444/restfuljersey").build();
}
}
Output 1:
Output 2:
FAQ
Other FAQs are mentioned below:
Q1. What are Jersey and JAX-RS?
Answer:
Jersey is a JAX-RS implementation, whereas JAX-RS is a standard (basically a definition). Therefore, the JAX-RS Reference Implementation is only one component of the Jersey framework. In addition, Jersey offers its API that adds additional features and tools to the JAX-RS toolset to make developing RESTful services and clients easier.
Q2. What are Jersey and RESTEasy?
Answer:
Jersey and RESTEasy are two frameworks that have evolved to make it easier to construct RESTful web services and applications in Java. Two of the most widely used JAX-RS implementations are these frameworks. The creation of REST APIs is made more accessible by the excellent feature sets offered by both frameworks.
Q3. In RESTful web services, what does Jersey mean?
Answer:
Sun’s production-quality reference version of JAX-RS, or the Java API for RESTful Web Services, is called Jersey. The JSR-311 annotations are supported by Jersey, which makes it simple for programmers to create RESTful web services using Java and the Java JVM.
Q4. Why do we utilize Jersey?
Answer:
An open-source framework for creating RESTful Web Services is called Jersey. It performs the role of a reference JAX-RS implementation. This post will examine how to use Jersey to create a RESTful Web Service. Additionally, we’ll combine Java configuration with Spring’s Dependency Injection (DI).
Conclusion
It is a framework for creating web services and developing applications. It makes API easy for complicated desktop apps.
Recommended Articles
This is a guide to JAX-RS Jersey. Here we discuss the introduction, Installation, RESTful web services, and examples. You may also have a look at the following articles to learn more –