在异常映射器中检索请求正文
我正在尝试在 JAX-RS ExceptionMapper 中检索请求的正文。到目前为止,这是我的代码:
@Provider @Componenet
public class BaseExceptionMapper implements ExceptionMapper<Exception> {
@Context private HttpServletRequest request;
@Override
public Response toResponse(Exception ex) {
// Trying to retrieve request body for logging throws an error
String requestBody = IOUtils.toString(request.getInputStream());
}
}
所以我的困境是我无法获取用于记录的请求正文,因为 servlet API 不允许您多次调用 request.getInputStream() / request.getReader() 来请求(和 JAX -RS 显然是调用它来解析请求)。有谁知道是否有办法做我想做的事情?
I'm trying to retrieve the body of a request in a JAX-RS ExceptionMapper. Here is my code so far:
@Provider @Componenet
public class BaseExceptionMapper implements ExceptionMapper<Exception> {
@Context private HttpServletRequest request;
@Override
public Response toResponse(Exception ex) {
// Trying to retrieve request body for logging throws an error
String requestBody = IOUtils.toString(request.getInputStream());
}
}
So my dilemma is I can't get the request body for logging because the servlet API wont allow you to call request.getInputStream() / request.getReader() more than once for a request (and JAX-RS Is obviously calling it to parse the request). Does anyone know if there is a way to do what I'm trying to do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这个问题有点老了,但答案仍然可能对其他人有帮助。我的示例还依赖于 Commons-Io。
您可以创建一个ContainerRequestFilter并使用TeeInputStream来代理/复制原始InputStream:
并在ExceptionMapper中使用@Inject和javax.inject.Provider来注入ContainerRequest。
ExceptionMapper 看起来像这样:
当我也使用 @Component 注释时,我的 ExceptionMapper 没有被使用。我认为@Provider 就足够了。
This question is a bit older, but still the answer may help others. My Example also depends on Commons-Io.
You can create a ContainerRequestFilter and use TeeInputStream to proxy/copy the original InputStream:
And use @Inject with javax.inject.Provider in your ExceptionMapper to get the ContainerRequest injected.
The ExceptionMapper would look like this:
When I have also used the @Component annotation my ExceptionMapper was not used. I think that @Provider is sufficient.
一种可能的解决方案是使用 servlet 过滤器并包装请求,这允许您拦截对请求输入流的读取调用。示例伪代码(取决于
commons-io
):Filter:
Mapper:
您需要一个
ServletInputStream
的包装类,您可以在此处找到示例实现:< a href="https://stackoverflow.com/questions/681263/modify-httpservletrequest-body">修改 HttpServletRequest 正文One possible solution is to use a servlet filter and wrap the request, which allows you to intercept read calls to the request input stream. Example pseudo-code (depends on
commons-io
):Filter:
Mapper:
You'll need a wrapper class for
ServletInputStream
, and you can find an example implementation here: Modify HttpServletRequest body我知道这是一个老问题,但我找到了一个我认为很高兴分享的解决方法。
通过以下代码,您应该能够在 ExceptionMapper 中获取 ContainerRequestContext ,然后您可以读取正文、查询参数、标头等。
希望它能有所帮助
I know this is an old question but I found a workaround that I think it's nice to share.
With the following code you should be able to get the
ContainerRequestContext
inside theExceptionMapper
, then you can read the body, query params, headers, etc.Hope it can help