繁体   English   中英

球衣/ JAX-RS ExceptionMapper MySQL

[英]Jersey / JAX-RS ExceptionMapper MySQL

我正在学习Jersey / JAX-RS,并且在ExceptionMapper方面需要一些帮助。

我有一个UserFacade类,AbstractFacade类和User类本身,它们都是非常标准的,主要是通过使用Netbeans中的Database创建一个新的Web Service RestFUL项目而生成的。 我的问题是,我现在想开始捕获错误,例如说“ Unique Constraint Violation”错误。 我以为我需要实现一个异常映射器...我的外观中包含以下内容:

@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();
        }
    }

这是我得到的错误,我的自定义异常处理程序未捕获该错误。

WARNING:   StandardWrapperValve[service.ApplicationConfig]: Servlet.service() for servlet service.ApplicationConfig threw exception
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry 'usernamegoeshere' for key 'username'

我觉得我已经接近了,我没有尝试从上面的示例中捕获MySQLIntegrityConstraintViolationException的唯一原因是,因为我只是试图首先捕获所有可能的错误(以确保其正常工作),所以我将缩小范围并在看到语法有效后再进行具体说明。

我究竟做错了什么?

  1. 总是参数化ExceptionMapper

     public class EntityNotFoundMapper implements ExceptionMapper<PersistenceException> { ... } 
  2. MySQLIntegrityConstraintViolationException似乎没有扩展PersistenceException 要捕获MySQLIntegrityConstraintViolationException您需要直接为此类或其前任之一创建ExceptionMapper ,例如:

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

    或更通用的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