What does a JAX-RS provider mean?

Can someone explain to me what the JAX-RS Provider is and what the @Provider annotation is. I read the documentation, but I can not get it.
If there are resource classes that serve incoming requests, what do providers do? How do they differ from single-level resource classes when I create a persistent resource class (one that is not intended for a query)? Or are these classes also suppliers?

+59
java jersey jax-rs
Nov 26 '12 at 1:17
source share
2 answers

Providers are simply a way to extend and customize the JAX-RS runtime. You can think of them as plugins that (potentially) modify the behavior of the runtime environment in order to fulfill a set of goals (defined by the program).

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

There are three classes of vendors 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 declaring a constructor. In addition, different types of providers may have additional annotations and will implement different interfaces.




Suppliers noun

These providers control the mapping of data representations (e.g. XML, JSON, CSV) with their Java object equivalents.

Context providers

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

Exception providers

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




Your runtime will have many predefined providers that will be responsible for implementing the basic level of functionality (for example, to map to and from XML, translate the most common exceptions, etc., etc.). You can also create your own suppliers as needed.

The JAX-RS specification is a good reference for reading these different types of providers and what they do (see chapter 4).

+83
Nov 26
source share

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

@Provider gives you the ability to check incoming and outgoing messages at the raw XML level, and thus the provider is the sending partner on the client.

+5
Jun 13 '15 at 12:53 on
source share



All Articles