December 15, 2015

Jersey API Exception Handler


In Jersey, you can register your own exception handler so can return an appropriate HTTP response for any exception In the handler, you can set the error response or convert it to any JSON object.





@Provider
public class ApiExceptionHandler implements ExceptionMapper<Exception> {

     @Override
     public Response toResponse(Exception exception) {
        // TODO : insert logger statement
        return Response.status(Status.BAD_REQUEST).entity(exception.getMessage()).build();
     }

}

No comments: