Table of contents
1.
Introduction
2.
JAX RS Annotation
3.
Annotation Examples
3.1.
FormParam Annotation
3.2.
QueryParam Annotations
3.3.
DefaultValue Annotations
3.4.
PathParam Annotation
4.
Frequently Asked Questions
4.1.
What is the use of JAX-RS?
4.2.
What are annotations in RESTful web services?
4.3.
Is it possible to use JAX-RS in Spring Boot?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

JAX RS Annotation Examples

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

The JAX-RS API uses annotations to offer meta-data about the web resource. A common example is to use the "@GET" annotation in conjunction with the "@Path" annotation to determine the method that should handle a GET request to the provided URI in the "@Path" annotation. JAX-RS additionally includes annotations that allow us to utilise different methods based on the request and response media types. The request body's media type (Content-Type header) gets checked against the value(s) of the "@Consumes" annotations. We will see different annotations to develop RESTful applications in java below.

JAX RS Annotations

JAX RS Annotation

So, let's talk about why analytics are so important in social media marketing. Every major social platform provides analytics reports to show us how effective our social media marketing is performing, along with that where we can make improvements.

JAX RS Annotation

Annotation Examples

We will also see a few JAX RS annotations example and their implementation below.

FormParam Annotation

Below is an example of using @FormParam to link HTML form fields to method inputs. It works using the HTTP method POST.

HTML form:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
                                    "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Coding Ninjas Student Registration Form</title>
</head>
<body>
    <h1>Enter Student Details</h1>
    <form action="rest/student-form/enroll" method="post">
        <p>roll : <input type="text" roll="Enter Roll Number" /></p>
        <p>school : <input type="text" school="School Name" /></p>
        <input type="submit" value="Submit Student Details" />
    </form>
</body>
</html>

Enroll:

package com.codingNinjas.restful; 
 
import javax.ws.rs.POST;
import javax.ws.rs.FormParam;
import javax.ws.rs.core.Response;
import javax.ws.rs.Path;
 
@Path("/user-form")
public class cnStudentReg {
 
    @POST
    @Path("/register")
    public Response studentRegistration(@FormParam("roll") String roll,
                                    @FormParam("school") String school){
         
        String response = "enrolled student information successfully, roll: "+
                            roll+" and school: "+school;
        return Response.status(200).entity(response).build();
    }
}
You can also try this code with Online Java Compiler
Run Code

Input Form

The Input form for students

Output

Output after Submitting

QueryParam Annotations

Here's an example of using the JAX-RS @QueryParam and @DefaultValue annotations to inject values from request parameters into our method input parameters.

Here we have two query parameters : Country and City

package com.codingNinjas.restful;
 
import javax.ws.rs.QueryParam;
import javax.ws.rs.GET;
import javax.ws.rs.core.Response;
import javax.ws.rs.Path;
 
@Path("/geography")
public class ExampleQueryParam {
 
    @GET
    @Path("/query")
    public Response getgeographyQuery(@QueryParam("country") String country,
                                    @QueryParam("city") String city){
        String resp = "received the Query parameters. 'country' value is: "
                        +country+" and the city is: "+city;
         
        return Response.status(200).entity(resp).build();
    }
}
You can also try this code with Online Java Compiler
Run Code

DefaultValue Annotations

The @DefaultValue annotation can be used to specify the default value.

package com.codingNinjas.restful;
 
import javax.ws.rs.QueryParam;
import javax.ws.rs.GET;
import javax.ws.rs.core.Response;
import javax.ws.rs.Path;
 
@Path("/geography")
public class ExampleQueryParam {
 
    @GET
    @Path("/query")
    public Response getgeographyQuery(@QueryParam("country") String country,
                                    @QueryParam("city") String city){
        String resp = "received the Query parameters. 'country' value is: "
                        +country+" and the city is: "+city;
         
        return Response.status(200).entity(resp).build();
    }
}
You can also try this code with Online Java Compiler
Run Code

PathParam Annotation

In the following example, we will use the JAX-RS @PathParam annotation to inject value from the URI into our method input parameters.

package com.codingNinjas.restful;
 
import javax.ws.rs.PathParam;
import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.core.Response;
 
@Path("/geography")
public class StudentService {
 
    @GET
    @Path("{StudentId}")
    public Response getStudentById(@PathParam("StudentId") String StudentId){
         
        return Response.status(200).entity("found student with id : " + StudentId).build();
    }
     
    @GET
    @Path("{year}/{branch}")
    public Response getStudentList(@PathParam("Yeah") String studyYear,
                                    @PathParam("branch") String branchName){
         
        String resp = "The full number of students in the "+deptName+" department from "
                        +branchName+" is 100";
        return Response.status(200).entity(resp).build();
    }
}
You can also try this code with Online Java Compiler
Run Code

In the example given above, if we use "/student/199" URI pattern, getStudentById() method will be invoked, and you will get "found student with id : 199" as an outcome.

Frequently Asked Questions

What is the use of JAX-RS?

JAX-RS is a JAVA-based programming language API & protocol that provides support for creating RESTful Web Services. Its version 2.0 was launched on May 24th, 2013. To simplify the construction and deployment of JAVA-based web services, JAX-RS makes use of annotations provided in Java SE 5.

What are annotations in RESTful web services?

Annotations are similar to meta-tags in that they can be added to code and applied to type declarations, package declarations, methods, constructors, fields, arguments, and variables.

Is it possible to use JAX-RS in Spring Boot?

Spring Boot provides great JAX-RS web service functionality. There are often two JAX-RS implementations to select from. Jersey and Apache CXF are two examples.

Conclusion

In the article, we read about various JAX RS Annotation examples. We also ran them and saw how they can get used in real-life scenarios. We listed down different annotations with their description and uses. Refer to our courses and explore Coding Ninjas Studio to find more exciting stuff. You can also look into the interview experiences and solve different problems. Look into our Guided paths, test series, libraries and resources to know more.

Thank You

Happy Coding!

Live masterclass