users@jersey.java.net

[Jersey] Re: Method dispatch based on MediaType

From: Pavel Bucek <pavel.bucek_at_oracle.com>
Date: Mon, 2 Nov 2015 11:44:23 -0800

Hi Kevin,

for the first part: you can use "quality" params in produces
annotations, something like:

@Produces("application/json;qs=0.75")

(see
https://github.com/jersey/jersey/blob/b7907e279010e7035a7a3e529993d22f77a21e08/tests/e2e/src/test/java/org/glassfish/jersey/tests/e2e/server/ContentNegotiationTest.java
; this is taken from Jersey 2.x, but it should work in Jersey 1.x as well).

more formal definition can be found at:

http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.9


The second one would be harder to do automatically, but you can always
do @Produces("*/*, qs=0,1"), which will be matched when
"application/pdf" is requested and can return
Response.status(406).build() or something like that..

Hope it helps,
Pavel


On 02/11/15 11:29, Kevin Hale Boyes wrote:
> I have a resource with a couple of "search" methods.
> They can produce their output in either JSON or XML based on the
> request Accept header.
>
> So, my methods basically look like this:
>
> @GET @Produces(MediaType.APPLICATION_JSON)
> public Response searchReturningJSON(@Context HttpServletRequest request) {
> return performSearch(request, MediaType.APPLICATION_JSON);
> }
>
>
> @GET @Produces(MediaType.APPLICATION_XML)
> public Response searchReturningXML(@Context HttpServletRequest request) {
> return performSearch(request, MediaType.APPLICATION_XML);
> }
>
> I'm hoping someone can help my with two things that I don't know how
> to do.
>
> I would like to have a default of JSON when the client gives */* for
> the Accept header.
> I would also like to (continue to) return a 406 error if the client
> asks for something different, like application/pdf.
>
> I'm using Jersey 1.19 in case it matters.
>
> Thanks,
> Kevin.