Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read the contents of an OutboundJaxrsResponse in Java

I have written a service that uses Jersey. In the last class that returns a Response object, I have:

Stream<Revision> revisionss = dataSourceHandler.getRevisions( .... );
StreamingOutput stream = os -> objectHistoryWorker.revisionsTransfer(revisions, os);
return Response.ok(stream).build();

This gets a Stream of Revision objects, converts the stream into a StreamingOutput, and then sends it out in the Response.

I'm trying to write an integration test to test this, and I want to see what contents are actually inside of the Response. In other words, I want to know information such as

  • How many Revision objects exist
  • Does a Revision object contain the correct information

The issue I'm having is that it is an OutboundJaxrsResponse, and the readEntity() method is not supported for it.

It has methods that will return whether it passed or not (i.e. status code 200), but I can't seem to figure out a way to actually read the contents of the Response.

Is there a way to get that information?

(The expected response content will be in Json format)

like image 658
user2869231 Avatar asked Oct 19 '22 09:10

user2869231


1 Answers

The class org.glassfish.jersey.message.internal.OutboundJaxrsResponse does support getEntity().

How to get the response contents as a Java String. From this, I'm sure you can work out how to read it as Json or XML ....

private String entity( Response response ) throws WebApplicationException, IOException
{
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    StreamingOutput output = (StreamingOutput) response.getEntity();
    output.write( baos );
    return baos.toString( "UTF-8" );
}
like image 87
Stewart Avatar answered Oct 21 '22 04:10

Stewart