I'm using Jersey's integrated Jackson processing to transform incoming JSON to a POJO, e.g.:
@POST @Consumes(MediaType.APPLICATION_JSON) public Response newCustomer( CustomerRepresentation customer) { ... }
If a client sends JSON with invalid fields Jersey currently returns a 500 Internal Server Error
. Instead, I'd like to return a 400 Bad Request
, preferably with some meaningful detail indicating which fields are in error.
Any insight into how this could be accomplished? (At least returning a generic 400 instead of the completely inappropriate 500?)
Update: Here's the exception being generated server-side, before my handler is invoked:
javax.servlet.ServletException: org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "this_isnt_a_known"_field" (Class com.redacted....), not marked as ignorable
I was finally able to work around this problem by implementing an ExceptionMapper
to catch the UnrecognizedPropertyException
thrown by Jackson and map it to a 400 Bad Request response:
@Provider public class UnrecognizedPropertyExceptionMapper implements ExceptionMapper<UnrecognizedPropertyException> { @Override public Response toResponse(UnrecognizedPropertyException exception) { return Response .status(Response.Status.BAD_REQUEST) .entity( "'" + exception.getUnrecognizedPropertyName() + "' is an unrecognized field.") .type( MediaType.TEXT_PLAIN) .build(); } }
I tried mapping status 500 to status 400 with HolySamosa's answer but the exception was not caught by this mapper, and status 500 was still being returned.
After debugging I found that JsonParseException is being thrown and not UnrecognizedPropertyException. This is because I was sending some garbage text (that was not JSON at all).
When I sent a proper JSON from client side, with format that was not appropriate for my DTO on the server side, then I got UnrecognizedPropertyException. So there are two cases for this:
Now I am returning status 400 for both.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With