Reputation: 276
I'm in the process of learning Jersey / JAX-RS and I need a bit of help with ExceptionMapper.
I've got a UserFacade Class, AbstractFacade Class and the User class itself, all pretty standard, mostly generated from just creating a new Web Service RestFUL project with Database in Netbeans. My problem, is that I want to now start catching errors, say "Unique Constraint Violation" errors. I thought I needed to implement an exception mapper... I have the following in my facade:
@Provider public class EntityNotFoundMapper implements ExceptionMapper { @Override public javax.ws.rs.core.Response toResponse(PersistenceException ex) { return Response.status(404).entity(ex.getMessage()).type("text/plain").build(); } }
This is the error I get, that is NOT caught by my custom exception handler.
WARNING: StandardWrapperValve[service.ApplicationConfig]: Servlet.service() for servlet service.ApplicationConfig threw exception com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry 'usernamegoeshere' for key 'username'
I feel like I'm close, the only reason i'm not trying to catch MySQLIntegrityConstraintViolationException from the example above, is because I'm just trying to catch every possible error FIRST (to make sure its working), then I'll narrow and be specific after I see that the syntax is working.
What am I doing wrong?
Upvotes: 2
Views: 1757
Reputation: 10379
Always parametrize ExceptionMapper
:
public class EntityNotFoundMapper
implements ExceptionMapper<PersistenceException> { ... }
MySQLIntegrityConstraintViolationException doesn't seem to be extending PersistenceException. To catch MySQLIntegrityConstraintViolationException
you need to create an ExceptionMapper
directly for this class or for one of it's predecessors, e.g.:
@Provider
public class MySqlIntegrityMapper
implements ExceptionMapper<MySQLIntegrityConstraintViolationException> {
@Override
public Response toResponse(MySQLIntegrityConstraintViolationException ex) {
return ...;
}
}
or for more generic SQLException (as MySQLIntegrityConstraintViolationException
inherits from it):
@Provider
public class SqlExceptionMapper implements ExceptionMapper<SQLException> {
@Override
public Response toResponse(SQLException ex) {
return ...;
}
}
Upvotes: 3