Table of contents
1.
Introduction
2.
Java Web Services API
3.
Inclusion in Java EE 
4.
Your Server Recognition
5.
An Example
6.
File Download Example: RESTful JAX-RS 
7.
RESTful JAX-RS File Upload Example
8.
Frequently Asked Questions
8.1.
What is a RESTful API?
8.2.
What is a web service?
8.3.
What are the advantages of RESTful web services?
9.
Conclusion
Last Updated: Mar 27, 2024

What is JAX-RS?

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

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. 

 

Java Web Services API

Java Logo

 

There exist two main java web services API: JAX-WS and JAX-RS. It performs communication through WSDL (Web Services Description Language). There are two ways to write its code: SOAP and RESTful.

1) JAX-WS: It is used for SOAP web services. RPC style and Document style are the two ways to write JAX-WS application code.

2) 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.

 

Let us try to focus on clarifying the confusion between JAX-RS and its implementations. We will also look at an example of a proper JAX-RS web app.

JAX-RS Flowchart

Inclusion in Java EE 

JAX-RS is like a set of interfaces and annotations that Java EE (Enterprise Edition) offers, and indeed we have the implementations; some of the more well-known are RESTEasy and Jersey.

Let us think of a case where you want to build an application server that is JEE-compliant, and then your server has to provide a JAX-RS implementation for the deployed apps to use. That is why it is called a Java Enterprise Edition Platform.

Your Server Recognition

Generally, it is advised that the platform we are using, we should take advantage of the platform that we're offered. At least for Production environments, we should see what JAX-RS implementation (like name, vendor, version, and known bugs) exists in the server before deciding to use it. For example, Wildfly or Jboss comes with RESTEasy, while Glassfish comes with Jersey.

So, at the beginning of the project or when you are migrating to another server, you should, at least once, have a piece of knowledge about all the points discussed above.

An Example

Let us see an instance where we are using JAX-RS. There are certain ways, but the shortest path is: to have a Maven web app project with the following dependency in the pom.xml:

<dependency>
    <groupId>javax</groupId>
    <artifactId>javaee-api</artifactId>
    <version>7.0</version>
    <scope>provided</scope>
</dependency>
You can also try this code with Online Java Compiler
Run Code

 

We have used JavaEE 7 because there are already plenty of application servers implementing it. Most of the annotations that you need to use are present in the API jar, which is located in package javax.ws.rs. The scope is "provided" because this jar is required at compile time and doesn't need to be in the final build. The server provides it for the run time.

After successfully adding the dependency, the entry class is first written: an empty class that extends javax.ws.rs.core. Application and is annotated with javax.ws.rs.ApplicationPath:

@ApplicationPath("/api")
public class RestApplication extends Application {
}
You can also try this code with Online Java Compiler
Run Code

 

We have declared the entry path as being /api. The other ways we declare for our resources, will be prefixed with /api.

Next, let's see a resource:

@Path("/notifications")
public class NotificationsResource {
    @GET
    @Path("/ping")
    public Response ping() {
        return Response.ok().entity("Service online").build();
    }
 
    @GET
    @Path("/get/{id}")
    @Produces(MediaType.APPLICATION_JSON)
    public Response getNotification(@PathParam("id") int id) {
        return Response.ok()
          .entity(new Notification(id, "jack", "test notification"))
          .build();
    }
 
    @POST
    @Path("/post/")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Response postNotification(Notification notification) {
        return Response.status(201).entity(notification).build();
    }
}
You can also try this code with Online Java Compiler
Run Code

File Download Example: RESTful JAX-RS 

JAX-RS API in Java can be used to download various types of files like text, images, pdf, etc. Just a few lines of code can do this.

You need to specify the different content types to download additional files. The @Produces annotation is used to identify the type of file content.

  1. @Produces("text/plain"): for downloading text file.
  2. @Produces("image/png"): for downloading png image file.jo
  3. @Produces("application/pdf"): for downloading PDF file.
  4. @Produces("application/vnd.ms-excel"): for downloading excel file.
  5. @Produces("application/msword"): for downloading ms word file.

RESTful JAX-RS File Upload Example

We saw in the previous topic that we can download files; likewise, we can easily upload a file. The @FormDataParam("file") annotation mentions the file parameter in the service class. The @Consumes(MediaType.MULTIPART_FORM_DATA) provides information about the file upload.

Frequently Asked Questions

What is a RESTful API?

A RESTful API can be defined as an architectural style that is primarily used for an application program interface (API). HTTP requests are used to access and, use data. That data can be used to GET, PUT, POST and, DELETE data types, which refers to the reading, updating, creating and deleting of operations concerning resources.

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 application.

What are the advantages of RESTful web services?

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

Conclusion

So, we can conclude that JAX-RS is a powerful API, and your web server may already implement many of the things you may need. This makes it easy as you no more need to turn your deployable code into an unmanageable pile of libraries.

See Basics of C++ with Data StructureDBMSOperating 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!

 

THank you image

 

Live masterclass