users@jersey.java.net

[Jersey] Jersey is returning 500 instead of 400 when given an invalid request body

From: Jeremy Johnson <jeremy.johnson_at_imtc.gatech.edu>
Date: Tue, 27 Mar 2012 15:00:41 -0400

I'm using Jersey's integrated Jackson processing to transform incoming JSON to a POJO argument in the handler method, 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. It was indicated to me elsewhere that Jersey *should* return 400 if the parsing fails.

Any insight into what's going on or how I might convince Jersey to return the a proper 400 error instead of the completely inappropriate 500?

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....

Thanks!

Jeremy