我有一个自定义ExceptionMapper
,可以捕获我的自定义RuntimeException
。问题是,当我抛出自定义ExceptionMapper
时,Exception
不会被调用。
我已在@Provider
课程中设置ExceptionMapper
,并在web.xml
注册为:{/ p>
<context-param>
<param-name>resteasy.providers</param-name>
<param-value>com.package.myMapper</param-value>
</context-param>
我的映射器是:
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;
@Provider
public class ResponseExceptionMapper implements ExceptionMapper<ResponseException>{
@Override
public Response toResponse(ResponseException arg0) {
// TODO Auto-generated method stub
System.out.println("in my mapper");
return Response.status(513).entity(arg0.getMessage()).build();
}
}
我的例外:
public class ResponseException extends RuntimeException {
public ResponseException() {
super();
}
public ResponseException(String msg) {
super(msg);
}
public ResponseException(String msg, Exception e) {
super(msg, e);
}
}
我做错了什么?...
答案 0 :(得分:0)
尝试让您的ResponseException扩展Exception而不是RuntimeException。似乎resteasy和jboss正在捕捉你的异常,而不是你的提供者,我有完全相同的问题,它解决了扩展Exception:
public class ResponseException extends Exception {
public ResponseException() {
super();
}
public ResponseException(String msg) {
super(msg);
}
public ResponseException(String msg, Exception e) {
super(msg, e);
}
}
以及异常映射器的示例:
import org.apache.log4j.Logger;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;
@Provider
public class BadRequestExceptionMapper implements ExceptionMapper {
private static final Logger log = Logger.getLogger(BadRequestExceptionMapper.class);
@Override
public Response toResponse(BadRequestException e) {
log.info("Bad request: " + e.getMessage());
return Response.status(Response.Status.BAD_REQUEST).entity(e.getMessage()).build();
}
}