annotations used in JAX RS API
Annotations in the JAX-RS (Java API for RESTful Web Services) API play a crucial role in defining and configuring RESTful web services. These annotations provide metadata that the JAX-RS runtime uses to map Java methods to HTTP operations and define resource URIs, media types, and other behaviors.
![annotations used in JAX RS API annotations used in JAX RS API](https://www.testpreparationz.com/wp-content/uploads/2024/10/151-overall-1-1.png)
Table of Contents
Explanation
- 1. Â Purpose of Annotations :
- Annotations in JAX-RS simplify the development of RESTful services by allowing developers to express resource mappings, HTTP methods, content types, and other aspects directly in Java code.
- They provide a declarative approach to defining RESTful endpoints and handling HTTP requests and responses.
- 2. Â Key Annotations :
- @Path : Defines the base URI path for the resource class or method.
- @GET, @POST, @PUT, @DELETE : Maps HTTP GET, POST, PUT, and DELETE requests to corresponding Java methods.
- @Produces, @Consumes : Specifies the media types that a resource produces or consumes.
- @PathParam : Binds method parameters to path segment variables in URI templates.
- @QueryParam : Binds method parameters to query string parameters.
- @FormParam : Binds method parameters to form-encoded data from HTTP POST requests.
- @HeaderParam : Binds method parameters to HTTP header values.
- @DefaultValue : Specifies default values for method parameters.
- @Context : Injects contextual objects (e.g., UriInfo, Request, HttpHeaders) into resource classes or methods.
Example in Java
Here’s a Java example demonstrating the use of some of these annotations in a JAX-RS resource class:
Syntax
java
import javax.ws.rs.*;
import javax.ws.rs.core.*;
@Path("/books")
public class BookResource {
@GET
@Path("/{id}")
@Produces(MediaType.APPLICATION_JSON)
public Response getBookById(@PathParam("id") int id) {
// Logic to retrieve book by ID
Book book = findBookById(id);
if (book != null) {
return Response.ok(book).build();
} else {
return Response.status(Response.Status.NOT_FOUND).entity("Book not found").build();
}
}
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.TEXT_PLAIN)
public Response addBook(Book book, @Context UriInfo uriInfo) {
// Logic to add a new book
int newBookId = addNewBook(book);
UriBuilder uriBuilder = uriInfo.getAbsolutePathBuilder();
uriBuilder.path(Integer.toString(newBookId));
return Response.created(uriBuilder.build()).entity("Book added successfully").build();
}
// Additional methods and logic for other CRUD operations
private Book findBookById(int id) {
// Implementation to find a book by ID
return null; // Placeholder implementation
}
private int addNewBook(Book book) {
// Implementation to add a new book
return 1; // Placeholder implementation
}
}
Explanation of the Example
- @Path(“/books”) : Specifies that the BookResource class handles URIs starting with /books.
- @GET, @POST : Annotations for handling HTTP GET and POST requests, respectively.
- @PathParam(“id”) : Binds the id path parameter from the URI to the getBookById method parameter.
- @Produces(MediaType.APPLICATION_JSON) : Specifies that the method produces JSON content type for HTTP response.
- @Consumes(MediaType.APPLICATION_JSON) : Specifies that the method consumes JSON content type for HTTP request.
These annotations enable the BookResource class to define a RESTful API for managing books, including retrieving a book by ID (GET /books/{id}) and adding a new book (POST /books).