0

I've got this below restful webservice code. But when the webservice is accessed I'm getting "MIME media type application/pdf was not found". The docService.findByVersionId does return a "TestDoc" which holds the pdf content as byte[].

Can you please help me in fixing this problem?

@GET
    @Path("/getPdf/{versionId}")
    @Produces("application/pdf")
    public Response getPdfFile(@PathParam("versionId") final String versionId) {
        try {
            final TestDoc doc = this.docService.findByVersionId(versionId);
            final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            final BufferedOutputStream bos = new BufferedOutputStream(byteArrayOutputStream);
            final byte[] pdfContent = doc.getPdfDoc();
            bos.write(pdfContent);
            bos.flush();
            bos.close();
            return Response.ok(byteArrayOutputStream).build();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }

    }

error:

 Exception:
 2014-01-02 12:42:07,497 ERROR [STDERR] 02-Jan-2014 12:42:07  com.sun.jersey.spi.container.ContainerResponse write
 SEVERE: A message body writer for Java class java.io.ByteArrayOutputStream, and Java type class java.io.ByteArrayOutputStream, and MIME media type application/pdf was not found
Mehrdad
  • 708
  • 1
  • 17
  • 38
Guru
  • 31
  • 4
  • Can I see the code on your rest-client or are you using a browser to get to this pdf api? In general conditions, from my experience, the rest-client (whatever client you might be using) is not able to deserialize the response because you probably forgot to mention the TYPE of the response (in this case, application/pdf).Let's discuss more once you update the question with your rest-client code (if any) – shahshi15 Jan 02 '14 at 16:47
  • You might want to look here as well. This should answer your question: http://stackoverflow.com/a/3503704/3066911 – shahshi15 Jan 02 '14 at 16:53

1 Answers1

1

It seems that you can't use the ByteArrayOutputStream. The solution is to use StreamingOutput.

@GET
public Response generatePDF(String content) {
    try {
        ByteArrayOutputStream outputStream = service.generatePDF(content);
        StreamingOutput streamingOutput = getStreamingOutput(outputStream);

        Response.ResponseBuilder responseBuilder = Response.ok(streamingOutput, "application/pdf");
        responseBuilder.header("Content-Disposition", "attachment; filename=Filename.pdf");
        return responseBuilder.build();
    } catch (IOException e) {
        log.log(Level.SEVERE, e.getMessage(), e);
        return Response.serverError().build();
    }
}



private StreamingOutput getStreamingOutput(final ByteArrayOutputStream byteArrayOutputStream) {
    return new StreamingOutput() {
        public void write(OutputStream output) throws IOException, WebApplicationException {
            byteArrayOutputStream.writeTo(output);
        }
    };
}
semiintel
  • 147
  • 1
  • 6