我想捕获所有类型的,Throwable
以便客户端在出现异常时不会暴露在我们的内部堆栈跟踪中。
我使用ExceptionMapper<Throwable>
如下:
@Provider
public class MappableContainerExceptionMapper implements ExceptionMapper<Throwable>
{
@Override
public Response toResponse(Throwable exception)
{
return Response.status(Response.Status.BAD_REQUEST).entity("caught").build();
}
}
但是,在传递时Content-Type=asdfasdf
,我会向客户端显示以下堆栈跟踪:
javax.ws.rs.WebApplicationException: java.lang.IllegalArgumentException: Error parsing media type 'sdfsdfdvfv'
com.sun.jersey.server.impl.model.HttpHelper.clientError(HttpHelper.java:265)
com.sun.jersey.server.impl.model.HttpHelper.getContentType(HttpHelper.java:91)
com.sun.jersey.server.impl.model.HttpHelper.getContentType(HttpHelper.java:77)
com.sun.jersey.spi.container.ContainerRequest.getMediaType(ContainerRequest.java:651)
com.sun.jersey.spi.container.servlet.WebComponent.filterFormParameters(WebComponent.java:832)
com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:407)
com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:538)
com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:716)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
</pre></p><p><b>root cause</b> <pre>java.lang.IllegalArgumentException: Error parsing media type 'sdfsdfdvfv'
com.sun.jersey.core.impl.provider.header.MediaTypeProvider.fromString(MediaTypeProvider.java:79)
com.sun.jersey.core.impl.provider.header.MediaTypeProvider.fromString(MediaTypeProvider.java:53)
javax.ws.rs.core.MediaType.valueOf(MediaType.java:119)
com.sun.jersey.server.impl.model.HttpHelper.getContentType(HttpHelper.java:89)
com.sun.jersey.server.impl.model.HttpHelper.getContentType(HttpHelper.java:77)
com.sun.jersey.spi.container.ContainerRequest.getMediaType(ContainerRequest.java:651)
我也想抓住这一点,以便向客户端显示漂亮的 JSON。