What are JAX-RS Annotations? (Part 3)
Overview of JAX-RS Annotations (Part 3)
This is a three-part series looking at the annotation that is used to implement REST endpoints.
In part two of JAX-RS annotations you learnt about:
- The @Path Annotation and @PathParam
- The @QueryParamter Annotation
- The @Produces Annotation
- The @Consumes Annotation
In this part, you will learn more about JAX-RS annotations. Are you ready? let’s get started.
The @FormParam Annotation
You may need to read the parameters sent in POST HTTP requests directly from the body, rather than serializing it to an object. This can be done by using the @FormParam
annotation.
@POST @Produces(MediaType.APPLICATION_JSON) public Response saveBookF(@FormParam("title") String title, @FormParam("author") String author, @FormParam("price") Float price) { return Response.ok(bookRepository.saveBook(new Book(title, author, price))).build(); }
The @MatrixParam Annotation
Matrix parameters are a set of query parameters separated by a semicolon rather than an ampersand. This may occur because the values were selected from a multiple select input box and being set via a GET request rather than a POST request. The URL might look something like this:
http://localhost:8080/api/books;author=atheedom;category=Java;language=english
The annotation @MatricParam
is used to retrieve the parameter value from the URI and assign it to a method parameter.
@GET @Produces(MediaType.APPLICATION_JSON) public Response getBookBy(@MatrixParam("author") String author, @MatrixParam("category") String category, @MatrixParam("language") String language) { return Response.ok( new GenericEntity<List<Book>>( bookRepository.getBookBy(author, category, language)) {}).build(); }
The @CookieParam Annotation
The @CookieParam
annotation allows you to inject directly into your resource method cookies sent by the client. Imagine you have sent a cookie called cartId
to the clients so that you can track the customer’s shopping cart. To pull the cookie from the HTTP request just annotate the method parameter to which you want the cookie data to be assigned.
@GET @Produces(MediaType.APPLICATION_JSON) public Response getCart(@CookieParam("cartId") int cartId) { return Response.ok().build(); }
The @HeaderParam Annotation
The @HeaderParam
annotation is used to inject HTTP request header values into resource method parameters. You can think of it like a shortcut using the @Context
annotation to inject the HttpServletRequest or HttpHeaders instance.
@GET @Produces(MediaType.APPLICATION_JSON) public Response getReferrer(@HeaderParam("referer") String referrer) { return Response.ok(referrer).build(); }
The @Provider Annotation
Providers are used to extend and customize JAX-RS by altering the behavior of the runtime to achieve a set of goals.
There are three types of providers:
- Entity Providers
This type of provider controls the mapping of data representations, such as JSON and XML, to their object equivalents - Context Providers
This type of provider controls the context that resources can access with the @Context annotation - Exception Providers
This type of provider controls the mapping of Java exceptions to a JAX-RS Response instance.
The only thing they have in common is that they must be identified by the @Provider
annotation and follow the correct rules for constructor declaration.
Code Repository
The source code for this article is in my GitHub repository. Code for all my articles is in the ReadLearnCode Articles repository.
Further Reading
If you are interested in reading more about the JAX-RS API then these articles will interest you:
- Bean validation failure management discusses how to handle response to clients when input fails data integrity checks
- Discover all the uses of the @javax.ws.rs.core.Context annotation
- working with @Consumes and @Produces annotations, and
- JAX-RS Resource Entities discusses how to create JAX-RS resource entities
Reference: | What are JAX-RS Annotations? from our JCG partner Alex Theedom at the Read Learn Code blog. |