Table of contents
1.
Introduction
2.
JAX-RS Server Code
2.1.
File1: Hello.java
2.2.
File2: index.html
2.3.
File3: web.xml
3.
JAX-RS Client Code
4.
Output
5.
Frequently Asked Questionss
5.1.
What is the use of JAX-RS?
5.2.
What is a web service?
5.3.
What are the advantages of RESTful web services?
6.
Conclusion
Last Updated: Mar 27, 2024

JAX-RS Jersey Example

Author Anmol Punetha
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?
OG Image

Introduction

Web service is a technology that one programming language uses to communicate with another. You can use web services to interact with PHP and .Net with Java. So, web service provides a way to achieve coordination and consistency. Here we will see an example of JAX-RS using jersey implementation.

In this example, jersey jar files have been used for using jersey example for JAX-RS. You may download the required jar files.

 

java

So there are two parts of the code, one for the server side and the other for the client one. We will use four different files, of which the first three files will form the server side while the last one will act for the client side.

  1. Hello.java
  2. index.html
  3. web.xml
  4. HelloWorldClient.java

JAX-RS Server Code

File1: Hello.java

package com.cn.rest;  
import javax.ws.rs.GET;  
import javax.ws.rs.Path;  
import javax.ws.rs.Produces;  
import javax.ws.rs.core.MediaType;  
@Path("/hello")  
public class Hello {  
  // This method is called if HTML and XML are not requested  
  @GET  
  @Produces(MediaType.TEXT_PLAIN)  
  public String sayPlainTextHello() {  
    return "Hello Jersey";  
  }  
  // This method is called if XML is requested  
  @GET  
  @Produces(MediaType.TEXT_XML)  
  public String sayXMLHello() {  
    return "<?xml version=\"1.0\"?>" + "<hello> Hello Jersey" + "</hello>";  
  }  
  
  // This method is called if HTML is requested  
  @GET  
  @Produces(MediaType.TEXT_HTML)  
  public String sayHtmlHello() {  
    return "<html> " + "<title>" + "Hello Jersey" + "</title>"  
        + "<body><h1>" + "Hello Jersey HTML" + "</h1></body>" + "</html> ";  
  }  
}   
You can also try this code with Online Java Compiler
Run Code

 

File2: index.html

<a href="rest/hello">Click Here</a>  

 

File3: web.xml

<?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="WebApp_ID" version="3.0">  
 <servlet>  
    <servlet-name>Jersey REST 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.cn.rest</param-value>  
    </init-param>  
    <load-on-startup>1</load-on-startup>  
  </servlet>  
  <servlet-mapping>  
    <servlet-name>Jersey REST Service</servlet-name>  
    <url-pattern>/rest/*</url-pattern>  
  </servlet-mapping>  
</web-app>

 

JAX-RS Client Code

package com.cn.restclient;  
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 ClientTest {  
  public static void main(String[] args) {  
    ClientConfig config = new ClientConfig();  
    Client client = ClientBuilder.newClient(config);  
    WebTarget target = client.target(getBaseURI());
    System.out.println(target.path("rest").path("hello").request().accept(MediaType.TEXT_PLAIN).get(String.class));
    System.out.println(target.path("rest").path("hello").request().accept(MediaType.TEXT_XML).get(String.class));
    System.out.println(target.path("rest").path("hello").request().accept(MediaType.TEXT_HTML).get(String.class));  
  }  
  private static URI getBaseURI() {  
    //here server is running on a 4444 port number, and the project name is jaxrsjersey 
    return UriBuilder.fromUri("http://localhost:4444/jaxrsjersey").build();  
  }  
} 
You can also try this code with Online Java Compiler
Run Code

Output

 

Output

Frequently Asked Questionss

What is the use of JAX-RS?

 It is primarily used for RESTful web services. There are mainly two implementations currently in use for creating JAX-RS applications: Jersey and RESTeasy.

What is a web service?

A Web Service is defined as a client-server application. It is the method of communication between two devices over the network and a collection of standards for exchanging information between two devices or applications.

What are the advantages of RESTful web services?

RESTful web services are fast, language and platform-dependent, and permit different data formats.

Conclusion

So, with this, we saw the JAX-RS Jersey example. I hope that the blog was informative.

See Basics of C++ with Data StructureDBMS, and Operating System by Coding Ninjas, and keep practicing on our platform Coding Ninjas Studio.

If you think you are ready for the tech giants company, check out the mock test series on code studio.

You can also refer to our Guided Path on Coding Ninjas Studio to upskill yourself in domains like Data Structures and AlgorithmsCompetitive ProgrammingAptitude, and many more! You can also prepare for tech giants companies like Amazon, Microsoft, Uber, etc., by looking for the questions asked by them in recent interviews. If you want to prepare for placements, refer to the interview bundle. If you are nervous about your interviews, you can see interview experiences to get ideas about these companies' questions.

Nevertheless, you may consider our premium courses to give your career an edge over others!

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!

 

OG image
Live masterclass