简体   繁体   English

球衣/ JAX-RS ExceptionMapper MySQL

[英]Jersey / JAX-RS ExceptionMapper MySQL

I'm in the process of learning Jersey / JAX-RS and I need a bit of help with ExceptionMapper. 我正在学习Jersey / JAX-RS,并且在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. 我有一个UserFacade类,AbstractFacade类和User类本身,它们都是非常标准的,主要是通过使用Netbeans中的Database创建一个新的Web Service RestFUL项目而生成的。 My problem, is that I want to now start catching errors, say "Unique Constraint Violation" errors. 我的问题是,我现在想开始捕获错误,例如说“ Unique Constraint Violation”错误。 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. 我觉得我已经接近了,我没有尝试从上面的示例中捕获MySQLIntegrityConstraintViolationException的唯一原因是,因为我只是试图首先捕获所有可能的错误(以确保其正常工作),所以我将缩小范围并在看到语法有效后再进行具体说明。

What am I doing wrong? 我究竟做错了什么?

  1. Always parametrize ExceptionMapper : 总是参数化ExceptionMapper

     public class EntityNotFoundMapper implements ExceptionMapper<PersistenceException> { ... } 
  2. MySQLIntegrityConstraintViolationException doesn't seem to be extending PersistenceException . MySQLIntegrityConstraintViolationException似乎没有扩展PersistenceException To catch MySQLIntegrityConstraintViolationException you need to create an ExceptionMapper directly for this class or for one of it's predecessors, eg: 要捕获MySQLIntegrityConstraintViolationException您需要直接为此类或其前任之一创建ExceptionMapper ,例如:

     @Provider public class MySqlIntegrityMapper implements ExceptionMapper<MySQLIntegrityConstraintViolationException> { @Override public Response toResponse(MySQLIntegrityConstraintViolationException ex) { return ...; } } 

    or for more generic SQLException (as MySQLIntegrityConstraintViolationException inherits from it): 或更通用的SQLException (因为MySQLIntegrityConstraintViolationException继承自它):

     @Provider public class SqlExceptionMapper implements ExceptionMapper<SQLException> { @Override public Response toResponse(SQLException ex) { return ...; } } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM