Reputation: 4183
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).
Upvotes: 1
Views: 1314
Reputation: 2797
Yes, avoid exception handlers, handle this case with methods:
@Path("example")
public class Example {
@GET
public Response getBadStuff() {
return Response.status(Response.Status.UNSUPPORTED_MEDIA_TYPE).build();
}
@GET
@Produces(MediaType.APPLICATION_JSON)
public Object getGoodStuff() {
return myObject;
}
}
Upvotes: 1