users@jersey.java.net

[Jersey] Retrieve Request Body in Exception Mapper

From: <bdicasa_at_gmail.com>
Date: Fri, 11 Nov 2011 16:54:38 +0000 (GMT)

Hey all,

I've got a question on stackoverflow that I figured I'd ask here as its
pretty specific. If you have a stackoverflow account you can answer the
question here:

http://stackoverflow.com/questions/8095631/retrieve-request-body-in-exc
eption-mapper

I'm trying to retrieve the body of a request in a JAX-RS (Jersey)
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?