119

Could anyone explain to me what a JAX-RS Provider is and what ‘@Provider’ annotation does? I have been reading documentation but I cant get it.
If there are resource classes that service the incoming requests, what do Providers do? How are they different from singleton resource classes when I create a persistent resource class (the one that is not per-request)? Or are those classes also providers?

bruno
  • 2,043
  • 17
  • 26
Artem Moskalev
  • 5,488
  • 11
  • 34
  • 52
  • To go along with this: Why doesn't the JAX-RS document explain this in the first paragraph of the "Providers" chapter—the logically page I turned to when looking for understanding. [JAX-RS Documentation PDF](https://www.google.at/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&ved=2ahUKEwjQvJGH1urhAhWnQxUIHZxTBdIQFjAAegQIAhAC&url=https%3A%2F%2Fdownload.oracle.com%2Fjavaee-archive%2Fjax-rs-spec.java.net%2Fjsr339-experts%2Fatt-3593%2Fspec.pdf&usg=AOvVaw3GTe0r3D6zl2A2jAv9Ra2B) – Jonathan Komar Apr 25 '19 at 07:01

3 Answers3

147

Providers are a simply a way of extending and customizing the JAX-RS runtime. You can think of them as plugins that (potentially) alter the behavior of the runtime, in order to accomplish a set of (program defined) goals.

Providers are not the same as resources classes, they exist, conceptually, at a level in-between resources classes and the JAX-RS implementation. If it helps, you can think of them in the same light as device drivers (existing between user and kernel space). This is a broad generalization.

There are three classes of providers defined by the current JAX-RS specification. The commonality between them is that all providers must be identified by the @Provider annotation and follow certain rules for constructor declaration. Apart from that, different provider types may have additional annotations, and will implement different interfaces.


Entity Providers

These providers control the mapping of data representations (like XML, JSON, CSV) to their Java object equivalents.

Context Providers

These providers control the context that resources can access via @Context annotations.

Exception Providers

These providers control the mapping of Java exceptions to a JAX-RS Response instance.


Your runtime will come with a number of predefined providers that will be responsible for implementing a base level of functionality (e.g for mapping to and from XML, translating the most common exceptions etc etc). You can also create your own providers as needed.

The JAX-RS specification is a good reference for reading up on these different provider types and what they do (see Chapter 4).

James
  • 10,392
  • 4
  • 46
  • 76
Perception
  • 75,573
  • 19
  • 170
  • 185
  • Thanks. I think I got the idea of the thing =) – Artem Moskalev Nov 26 '12 at 15:07
  • Well explained @Perception. This really helped in my understanding. – L-Samuels May 06 '14 at 20:29
  • Well explained. One question though - how are @provider implementations different from an implementation of javax.ws.rs.core.Feature interface injected through the init param (jersey.config.server.provider.classnames) in web.xml? How is the order controlled? – Andy Dufresne Jul 12 '17 at 05:22
  • Note the latest version of the JAX-RS specification (Version 2.1 Final Release July 13, 2017) https://download.oracle.com/otn-pub/jcp/jaxrs-2_1-final-spec/jaxrs-2_1-final-spec.pdf?AuthParam=1546559699_8a1c42b298288ed02e280d293e710306 – burntsugar Jan 04 '19 at 00:57
14

The @Provider annotation is used for anything that is of interest to the JAX-RS runtime, such as MessageBodyReader and MessageBodyWriter. For HTTP requests, the MessageBodyReader is used to map an HTTP request entity body to method parameters. On the response side, a return value is mapped to an HTTP response entity body by using a MessageBodyWriter. If the application needs to supply additional metadata, such as HTTP headers or a different status code, a method can return a Response that wraps the entity and that can be built using Response.ResponseBuilder.

@Provider annotation gives you the ability to examine incoming and outgoing messages at the raw XML level, and in this way Provider is the counterpart to Dispatch on the client.

A_BOSS
  • 424
  • 5
  • 8
9

To do certain activities such as Filtering-Request/Response, Exception Handling, the JAX-RS has its own default implementation logic. However, it allows users to provider thier own implementation as well.

To provide our own implementation we need to implement the appropriate classes by specifying them with @Provider annotation.

JAX-RS will do a round of scanning to find the existance of any such user-defined implementation by seaching for @Provider annotation.

For example:

...
@Provider
public class AppExceptionMapper implements ExceptionMapper<Throwable> {
...

...
@Provider
@PreMatching
public class RESTRequestResponseFilter implements ContainerRequestFilter, ContainerResponseFilter {
...
maris
  • 532
  • 5
  • 6