【发布时间】:2011-08-12 02:53:01
【问题描述】:
我想将底层RuntimeExceptions 包装成自定义 json 格式,使 servlet 容器不会将堆栈跟踪转储到客户端。
我关注这个问题:JAX-RS (Jersey) custom exception with XML or JSON。 调用时:
try {
doSomething(parameters);
}
catch(RuntimeException e) {
throw new MyCustomException(500 , e.getMessage() , Status.INTERNAL_SERVER_ERROR);
}
当我故意输入错误的参数(并触发 doSomething() 抛出的 RuntimeException )时,我没有看到 MyCustomExceptionMapper 工作。相反,servlet 容器转储:
The RuntimeException could not be mapped to a response, re-throwing to the HTTP container
api.MyCustomException: (underlaying msgs)
MyCustomExceptionMapper 确实在javax.ws.rs.core.Application 中注册:
@Override
public Set<Class<?>> getClasses()
{
Set<Class<?>> set = new HashSet<Class<?>>();
set.add(other classes);
set.add(MyCustomExceptionMapper.class);
return set;
}
我错过了什么?
非常感谢!
环境:JAX-RS,jersey-server 1.5
类规范:
class MyCustomException extends RuntimeException
@Provider
class MyCustomExceptionMapper implements ExceptionMapper<MyCustomException>
更新:
我怀疑 Application.getClasses() 从未被调用,所以我添加了一些 println 消息:
@Override
public Set<Class<?>> getClasses()
{
System.out.println("\n\n\n\n ApiConfig getClasses");
}
事实上,它从未显示出来!
我确定这个 ApiConfig 在 web.xml 中:
<context-param>
<param-name>javax.ws.rs.core.Application</param-name>
<param-value>destiny.web.api.ApiConfig</param-value>
</context-param>
但为什么泽西岛似乎从不调用它?
【问题讨论】: