On May 11, 2010, at 7:53 AM, Jan Algermissen wrote:
> Since this came up on SO([1]) I am wondering on what basis Jersey
> chooses the medi atype to serve when the client's preferences are
> ambigous.
>
> Example:
>
> - Resource class can serve text/html and image/gif
> - Accept header is text/html,image/gif
>
> Which media type would Jersey choose to send and why?
>
By default Jersey/JAX-RS will consider text/html more acceptable than
image/gif because the former occurs before the latter. So "text/html"
will be matched first against any @Produces declarations.
If say the accept header was "*/*" and one had:
@Produces("text/html,image/gif")
Then "text/html" will be selected.
If one had two resource methods:
@Produces("text/html") String x() { ... }
@Produces("image/gif") String y() { ... }
then results across platforms are ambiguous because the JVM does not
specify a method order when using reflection, in reality though it is
usually in declaration order.
Jersey supports quality of source parameters (TODO update the user
guide!), "qs", on the server side, that will order media types
declared in @Produces relative to other media types declared in
@Produces.
Thus if one has:
@Produces("text/html;qs=2") String x() { ... }
@Produces("image/gif") String y() { ... }
and an accept headers of:
Accept: image/gif, text/html
Accept: image/gif, text/html;q=0.8
Accept: image/gif, text/*
Accept: image/gif, text/*;q=0.8
Accept: image/gif, */*
Accept: image/gif, */*;q=0.8
then text/html will always be selected because all the above contain a
media type that matches text/html.
Paul.