How to return image as stream from JAX-RS?

Justin picture Justin · Aug 24, 2012 · Viewed 17.3k times · Source

I'm trying to return an image in a JAX-RS web service. I was able to get this successfully working by returning FileInputStream but I'd prefer to avoid creating a File for each request.

I am using Apache CXF and Jackson (all the other resource methods produce application/json).

Code looks like this:

@GET
@Produces("image/png")
public Response getQrCode(@QueryParam("qrtext") String qrtext) {

    ByteArrayOutputStream out = QRCode.from(qrtext).to(ImageType.PNG).stream();

    return Response.ok(out).build();
}

Unfortunately, this produces the dreaded:

org.apache.cxf.jaxrs.interceptor.JAXRSOutInterceptor:376 - No message body writer has been found for response class ByteArrayOutputStream.

Here's a link to a similar post but it doesn't mention the "No message body writer" issue I'm running into.

I'd appreciate any ideas of how to deal with this issue. Thanks!

Answer

StaxMan picture StaxMan · Aug 26, 2012

Just use StreamingOutput wrapper. For some reason it is unknown, but it is GREAT for providing, well, streaming output. :-)