我想包裹底层RuntimeExceptions
自定义 json 格式,使得 servlet 容器不会将堆栈跟踪转储到客户端。
我关注这个问题:使用 XML 或 JSON 的 JAX-RS (Jersey) 自定义异常 https://stackoverflow.com/questions/3227360/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);
}
当我故意输入错误的参数(并触发RuntimeException
被抛出doSomething()
),我没看到MyCustomExceptionMapper
在职的。相反,servlet 容器转储:
The RuntimeException could not be mapped to a response, re-throwing to the HTTP container
api.MyCustomException: (underlaying msgs)
The 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>
updated :
我怀疑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>
但为什么泽西岛似乎从来没有打电话过呢?