Do you think IIT Guwahati certified course can help you in your career?
No
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 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.
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.
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
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
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
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.