users@jersey.java.net

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

From: Suneel Marthi <suneel_marthi_at_yahoo.com>
Date: Tue, 27 Mar 2012 12:13:01 -0700 (PDT)

One way would be to throw this exception (or wrap it in a CustomException) from your method and have a custom @Provider handle this exception and then return a 400 response from the provider.

I am sure there are better ways (haven't been working with Jersey for a while now)



________________________________
 From: Jeremy Johnson <jeremy.johnson_at_imtc.gatech.edu>
To: "users_at_jersey.java.net" <users_at_jersey.java.net>
Sent: Tuesday, March 27, 2012 3:00 PM
Subject: [Jersey] Jersey is returning 500 instead of 400 when given an invalid request body
 

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