【发布时间】:2019-05-08 09:55:14
【问题描述】:
我们正在运行 DropWizard 并尝试记录导致抛出 404 响应的 URL
我们有一个接收NotFoundException 的包罗万象的异常映射器。令人沮丧的是,该异常不包含导致它被抛出的 URL 的上下文。
此处的示例应用程序:https://github.com/pauldambra/not.found.example
我们使用的是ExceptionMapper
public class NotFoundLogger implements ExceptionMapper<NotFoundException> {
ExampleLogger logger = new ExampleLogger();
@Override
public Response toResponse(final NotFoundException exception) {
logger.error(urlFrom(exception), exception);
return Response.status(404).build();
}
private String urlFrom(final NotFoundException exception) {
return "why is this not a property on the exception?!";
}
private class ExampleLogger {
void error(final String notFoundUrl, final NotFoundException exception) {
System.out.println("someone tried to load " + notFoundUrl);
System.out.println(exception.getMessage());
}
}
}
如果我们在有人请求应用程序不提供的 URL 时查看应用程序日志,我们会看到应用程序可以记录它正在为路径返回 404,但我们的自定义记录器无权访问该 URL
someone tried to load why is this not a property on the exception?!
HTTP 404 Not Found
127.0.0.1 - - [08/May/2019:09:53:47 +0000] "GET /ping/pong HTTP/1.1" 404
ExceptionMapper 是错误的方法吗?
【问题讨论】:
-
您能否访问在
toResponse()期间导致异常的HttpServletRequest?每stackoverflow.com/q/17766072/775715 -
或使用
UriInfoper stackoverflow.com/a/43610403/775715 -
哦,上下文机制看起来很有前途!我讨厌那些无法发现的魔法东西! (为什么不只是方法重载:D)
标签: java jax-rs jetty dropwizard