Exception exceptions not handled by ExceptionMapper

In the header, exceptions thrown from ParamConverter are NOT handled as I expect.

With ExceptionMapper:

@Provider public class MyExceptionMapper implements ExceptionMapper<MyException> { @Override public Response toResponse(MyException exception) { return Response.serverError().entity( "It triggered" ).build(); } } 

and ParamConverter:

 @Provider (boilerplate junk) @Override public DateTime fromString(String value) { throw new MyException("convert"); } 

It does not return the text "This is called" in error 500, but rather 404.

Expected question: are both providers registered?

Yes. If I throw a "MyException" from the resource (in the "regular" code), it works as expected. I can also convert seetrace with the message "convert".

Is there a way to make exceptions from ParamConverters by handling ExceptionMapper?

I am using jersey 2.3.1 with spring-jersey running in container for berth 9.1.0.RC0

+7
exception-handling jersey jax-rs
source share
2 answers

It seems like reading this , the JAX-RS specification says that the developer should wrap the unhandled exceptions in NotFoundException (404) for @QueryParam and @PathParam , and from what I tested 400, (I assume BadRequestException ) for @FormParam .

"if the field or property is annotated using @MatrixParam , @QueryParam or @PathParam , then the implementation MUST throw an instance of NotFoundException (status 404) that wraps the thrown exception and does not have an entity"

A couple of ways I can handle the exception are:

  • Just process it in ParamConverter , for example

     return new ParamConverter<T>() { @Override public T fromString(String string) { try { return (T)new MyObject().setValue(string); } catch (MyException ex) { Response response = Response.serverError().entity("Boo").build() throw new WebApplicationException(response); } } @Override public String toString(T t) { return t.toString(); } }; 
  • Or just throw a WebApplicationException and return a Response there. eg.

     public class MyException extends WebApplicationException { public MyException(String message) { super(Response.serverError().entity(message).build()); } } 
+2
source share

I experienced the same behavior in Jersey 2.26.

Any exception that raises a RuntimeException is mapped to a ParamException, which in itself is a subspace of the WebApplicationException. Assuming MyException extends RuntimeException, it doesn't get caught, because your ExceptionMapper only handles MyException.

Regarding documents in Jersey that throw a NotFoundException: I would say that 404 does not apply when queryParam cannot be converted. The BadRequestException seems to be more appropriate. And also, I do not see anything unique in the operation of the Jersey frame when the NotFoundException is thrown in addition to setting the response code

To get exceptions from the ParamConverter browser in ExceptionMapper, you will need to have your ExceptionMapper catch a more global exception like Throwable.

Another answer involves throwing a WebApplicationException. This should be a great solution, but will NOT work if the Response object has an object. See here: https://github.com/jersey/jersey/issues/3716

0
source share

All Articles