Hi
On Thu, Jan 20, 2011 at 1:52 PM, Trevor Bernard <[email protected]>wrote:
> Hi,
>
> When posting content to a JAX-RS end point, if the JSON serialization
> fails, I want the to return a bad request but the framework throws a
> 500 internal server error.
>
> I briefly evaluated ExceptionMappers but I couldn't get the
> functionality I wanted.
>
>
registering a custom ExceptionMapper should definitely work. Registering a
custom WebApplicationExceptionMapper or RuntimeException one should do it,
should catch the exceptions thrown by Jackson...
> Contrived examples:
>
> @POST
> @Consumes(MediaType.APPLICATION_JSON)
> @Path("/car")
> public Response postCars(Collection<Car> cars) {
> ....
> }
>
> public class Car {
> private final String name:
> public Cat(String name) {
> this.name = name;
> }
> }
>
> Lets say I this is the body of my post:
>
> [{ "name" : "Ford"}, { "color" : "Hot Pink" }]
>
> It should fail to serialize but is there a way for me to trap the
> error? Or map it differently?
>
>
You can use ExceptionMappers, CXF out fault interceptors, and finally you
catch the exception in the Servlet filter...
Please see
http://cxf.apache.org/docs/jax-rs-basics.html#JAX-RSBasics-Exceptionhandling
let me know if it works for you
Cheers, Sergey
> I can use an InputStream and serialize myself but I'd rather let the
> framework do the work for me.
>
> I'm using the jackson provider -
> org.codehaus.jackson.jaxrs.JacksonJaxbJsonProvider
>
> Warmest regards,
>
> Trev
>