users@jersey.java.net

[Jersey] Method dispatch based on MediaType

From: Kevin Hale Boyes <kcboyes_at_gmail.com>
Date: Mon, 2 Nov 2015 12:29:15 -0700

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.