News Ticker

What are JAX-RS Annotations?

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 one of JAX-RS annotations you learn about:

In this part, you will learn more about JAX-RS annotations. Are you ready, so let’s get started.

The @FormParam Annotation

You may need to read parameters sent in a POST HTTP requests directly from the body, rather than serializing it to an object. This can be done by using the @FormParam annotation.

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:


The annotation @MatricParam is used to retrieve the parameter value from the URI and assign it to a method parameter.

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.

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 to using the @Context annotation to inject the HttpServletRequest or HttpHeaders instance.

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:

Learn More

If you want to level up your Java EE skills you considered my online video training course. I cover a range of topics from the Java EE platform including:

However, if you are taking your first flurry into the awesome world of enterprise Java development, then you will want to take my course Learning Java Enterprise Edition. It is a chock-a-block 2hrs course covering all the most important APIs in the Java EE ecosystem.

1 Comment on What are JAX-RS Annotations?

5 Trackbacks / Pingbacks

  1. What are JAX-RS Annotations? Boost your career with us Professional Java EE Video Training and Tutorials
  2. Eclipse MicroProfile: 5 Things You Need to Know
  3. Getting Started with OSGi Remote Services – Bndtools Edition | vogella blog
  4. Getting Started with OSGi Remote Services – PDE Edition | vogella blog
  5. Getting Started with OSGi Remote Services – enRoute Maven Archetype Edition | vogella blog

Leave a Reply

%d bloggers like this: