Capture Response Payload in JAX-RS filter

codehammer

I want to capture and log the response payload in JAX-RS filter. Here is the code snippet of the filter method that I'm using to intercept the response. (FYI - I'm using RestEasy for implementation)

@Override
public void filter(final ContainerRequestContext requestContext, final ContainerResponseContext         responseContext) throws IOException {
    ...
      final OutputStream out = responseContext.getEntityStream();
       try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
               out.write(baos.toByteArray());
        ....
   }
}

However, the ByteArrayOutputStream turns out be empty. Looking at the RestEasy code, it's using DeferredOutputStream, but not sure how it would matter here in pulling the response payload. I have tried writing to byte[] directly, but that doesn't help either. Am I missing anything here ? Thanks.

lefloh

If you don't want to write more data to the response you don't need to deal with the OutputStream. Just use the response entity:

@Provider
public class SomeFilter implements ContainerResponseFilter {

    private Logger LOG = LoggerFactory.getLogger(SomeFilter.class);

    @Override
    public void filter(ContainerRequestContext requestContext,
            ContainerResponseContext responseContext) throws IOException {
        LOG.info("response entity: " + responseContext.getEntity());
    }

}

The OutputStream is empty at the time the Filter is called because the JAX-RS runtime has not written to it. After your Filter the runtime will choose the correct MessageBodyWriter which will serialize the entity to the OutputStream.

You could also intercept all MessageBodyWriters with a WriterInterceptor. Following example passes a ByteArrayOutputStream to the MessageBodyWriter and restores the original OutputStream afterwards:

@Provider
public class ResponseInterceptor implements WriterInterceptor {

    private Logger LOG = LoggerFactory.getLogger(ResponseInterceptor.class);

    @Override
    public void aroundWriteTo(WriterInterceptorContext context) throws IOException, WebApplicationException {
        OutputStream originalStream = context.getOutputStream();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        context.setOutputStream(baos);
        try {
            context.proceed();
        } finally {
            LOG.info("response body: " + baos.toString("UTF-8"));
            baos.writeTo(originalStream);
            baos.close();
            context.setOutputStream(originalStream);
        }
    }

}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to Read the JSON Payload in a JAX-RS Filter

From Dev

Jax-rs and servlet filter: IllegalStateException: Response already committed

From Dev

CORS Response Filter not invoked Resteasy / JAX-RS 2.0

From Dev

JAX-RS response filter > getLength() is always -1

From Dev

Jax-RS Multipart response

From Dev

Create Response with Location header in JAX-RS

From Dev

JAX-RS: Convert Response to Exception

From Dev

JAX-RS: Convert Response to Exception

From Dev

Retrieving String from JAX-RS Response

From Dev

JAX-RS filter not getting called

From Dev

Thread Safety in Jax-WS Request-Response Payload Logging

From Dev

Handling redirect Response from a Restful or JAX-RS web service

From Dev

Empty response for HTTP 503 with JAX-RS and Tomcat?

From Dev

Mapping JSON Response into Object with JAX-RS and JAXB

From Java

Read response body in JAX-RS client from a post request

From Dev

How to return List<String> as Response in JAX-RS

From Dev

Logging request and response in one place with JAX-RS

From Dev

Modifying the Apache cxf JAX RS response using outbound interceptor

From Dev

JAX-RS Response Object displaying Object fields as NULL values

From Dev

jax-rs Jersey @Post Response entity is empty

From Dev

return custom error codes to client in the response for a JAX-RS

From Dev

How to return Response in XML in JAX-RS web service?

From Dev

Handling custom error response in JAX-RS 2.0 client library

From Dev

JAX-RS 2.0 - How to get entity from Response object

From Dev

Customizing response of an object using jersy/JAX-RS REST

From Dev

How to close InputStream which fed into Response(jax.rs)

From Dev

JAX-RS response.getEntity returns null after post

From Dev

Java jax-rs client response entity generic

From Dev

Customizing JAX-RS response when a ConstraintViolationException is thrown by Bean Validation

Related Related

  1. 1

    How to Read the JSON Payload in a JAX-RS Filter

  2. 2

    Jax-rs and servlet filter: IllegalStateException: Response already committed

  3. 3

    CORS Response Filter not invoked Resteasy / JAX-RS 2.0

  4. 4

    JAX-RS response filter > getLength() is always -1

  5. 5

    Jax-RS Multipart response

  6. 6

    Create Response with Location header in JAX-RS

  7. 7

    JAX-RS: Convert Response to Exception

  8. 8

    JAX-RS: Convert Response to Exception

  9. 9

    Retrieving String from JAX-RS Response

  10. 10

    JAX-RS filter not getting called

  11. 11

    Thread Safety in Jax-WS Request-Response Payload Logging

  12. 12

    Handling redirect Response from a Restful or JAX-RS web service

  13. 13

    Empty response for HTTP 503 with JAX-RS and Tomcat?

  14. 14

    Mapping JSON Response into Object with JAX-RS and JAXB

  15. 15

    Read response body in JAX-RS client from a post request

  16. 16

    How to return List<String> as Response in JAX-RS

  17. 17

    Logging request and response in one place with JAX-RS

  18. 18

    Modifying the Apache cxf JAX RS response using outbound interceptor

  19. 19

    JAX-RS Response Object displaying Object fields as NULL values

  20. 20

    jax-rs Jersey @Post Response entity is empty

  21. 21

    return custom error codes to client in the response for a JAX-RS

  22. 22

    How to return Response in XML in JAX-RS web service?

  23. 23

    Handling custom error response in JAX-RS 2.0 client library

  24. 24

    JAX-RS 2.0 - How to get entity from Response object

  25. 25

    Customizing response of an object using jersy/JAX-RS REST

  26. 26

    How to close InputStream which fed into Response(jax.rs)

  27. 27

    JAX-RS response.getEntity returns null after post

  28. 28

    Java jax-rs client response entity generic

  29. 29

    Customizing JAX-RS response when a ConstraintViolationException is thrown by Bean Validation

HotTag

Archive