'Access to acceptableMediaTypes from JAX-RS resource

I understand how JAX-RS content negotiation works in the simple case of some server methods annotated with @Consumes and @Produces annotations.

I have a resource with a method that consumes both of the following types:

  • application/fhir+json
  • application/fhir+xml

For these media types, there is also a MIME-type parameter defined called fhirVersion. Now I am trying to add support for this parameter and I'd like to get ahold of the requested / acceptable MediaTypes (along with this parameter value) for the request.

Currently I'm getting the raw value of the Accept header from my code and parsing that into a new list of MediaTypes myself, but I feel like "there has to be a better way".

It looks like if I were in a ContainerRequestFilter, then I could get a list of the acceptable media types right from the ContainerRequestContext. Is there a way to get that on my JAX-RS resource instead?

I also found that I can inject a Request object on my JAX-RS resource class and that has a method selectVariant where I can pass it a list of variants (e.g. all possible combinations of my media types and this parameter value) and maybe get the right one back out. But that seems too magical for me (like do all the JAX-RS impls use the same logic and do they even pay attention to the MediaType parameters?).

Is there a way just to ask the framework "which MediaType did you use to get the request to this particular method" or, even better, get that injected somehow?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source