users@jersey.java.net

[Jersey] Re: How to catch the response in case of an invalid query paramter

From: Ryan Stewart <rds6235_at_gmail.com>
Date: Mon, 9 May 2011 17:22:44 -0500

Nice idea. If you were to rethrow the exception instead of mapping it, would
the default behavior still occur?

On Mon, May 9, 2011 at 4:28 PM, lasloo <lasloo_at_gmail.com> wrote:

> I think the answer you are looking for is registering a custom
> WebApplicationException mapper...
>
> @Provider
> public class CustomWebApplicationExceptionMapper implements
> ExceptionMapper<WebApplicationException>
> {
> public Response toResponse(WebApplicationException ex)
> {
> if ( ex.cause() instanceOf java.lang.NumberFormatException )
> {
> return Response.status(400).entity("Should have used a
> number!").build();
> }
> else { ... some default response instead .... }
> }
> }
>
> then in your spring bean file make sure to register it...
> <jaxrs:server ..... >
> <jaxrs:provider>
> <ref bean="customWebApplicationExceptionMapper"/>
> </jaxrs:provider>
> ....
> </jaxrs:server/>
>
> <bean id="customWebApplicationExceptionMapper"
> class="CustomWebApplicationExceptionMapper"/>
>
>
>
> --
> View this message in context:
> http://jersey.576304.n2.nabble.com/How-to-catch-the-response-in-case-of-an-invalid-query-paramter-tp6272285p6345730.html
> Sent from the Jersey mailing list archive at Nabble.com.
>