【发布时间】:2011-11-11 14:49:30
【问题描述】:
我正在尝试在 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 显然是调用它来解析请求)。有谁知道是否有办法做我想做的事?
【问题讨论】:
-
我猜您需要创建自定义异常,将已解析的实体包含在其中并在您的 ExceptionMapper 中重用该值..
标签: java servlets jersey jax-rs