I have a web application in which I throw some custom exceptions(application exceptions annotated with @ApplicationException
) and there is an exception mapper(provider annotated with @Provider
) for each. Recently I forgot to annotate an exception with @ApplicationException and still the mapper is able to identify the exception and format the response properly.
Then I checked the documentation and I understood that the annotation will be inherited by its child class by default. So I removed the annotation from the super class and checked. The mapper still identified the exception and formatted the response.
Then I went even forward and tried throwing java.lang.IllegalArgumentException
and wrote a mapper class for it. It also worked properly. Is javax.ws.rs.ext.ExceptionMapper
independent of the exception being thrown. Will it not check if whether thrown exception is really annotated with @ApplicationException
?
@Provider
public class IllegalArgumentExceptionMapper implements ExceptionMapper<java.lang.IllegalArgumentException> {
@Override
public Response toResponse(java.lang.IllegalArgumentException exception) {
return Response.status(Status.BAD_REQUEST).entity(exception.getMessage()).build();
}
}
Somewhere in my service class:
throw new java.lang.IllegalArgumentException("Problem with the payload. Please check the payload you are sending");