这是我的 SqlExceptionMapper
@Provider
public class SqlExceptionMapper implements ExceptionMapper<SQLException> {
@Override
public Response toResponse(SQLException e){
return ResponseBuilder.error(Response.Status.UNAUTHORIZED.getStatusCode(), "success", null, e.getMessage());
}
}
在这里,我在函数的 catch 块中抛出异常
public void myfunction()throws SQLException {
try {
// some code that throws an sql exception
} catch (SQLException e) {
final Logger logger = Logger.getLogger(Image.class);
logger.error("This is error", e);
throw new SQLException();
}
}
这是我的 web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application -image Rest API's</display-name>
<servlet>
<servlet-name>jersey-serlvet</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.image.controller;com.image.exceptionmapper</param-value>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey-serlvet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
我已经调试了我的代码并且我的 catch 块正在抛出 SQLException,唯一的问题是它没有映射到我创建的映射器。