users@jersey.java.net

[Jersey] How to handle requests with unsupported content type correctly?

From: Oliver B. Fischer <oliver.fischer_at_profitbricks.com>
Date: Fri, 20 Sep 2013 09:56:08 +0200

Dear list,

I have a REST interface build with Jersey. Actually I only support as
content type only application/json for all incoming requests. That is
why I defined my message body reader and writer as

@Provider
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class MarshallerProvider
     implements MessageBodyReader<Object>, MessageBodyWriter<Object>
{
}

Now I wrote a test where I try to get a document via GET and expected
content type application/xml. Jersey answers this request with an
MessageBodyProviderNotFoundException.

So what would be the best way to handle such unsupported requests
correctly? Should I write an exception mapper? Since it is an internal
exception I don't like this approach.

The solution should allow me to return an HTTP 415 (Unsupported Media Type).

Bye,

Oliver

-- 
Oliver Fischer
M +49 178 7903538
E oliver.fischer_at_profitbricks.com
W http://www.profitbricks.com
J oliver.fischer_at_profitbricks.com