Web services 在restful Web服务中未找到IME媒体类型应用程序/pdf

Web services 在restful Web服务中未找到IME媒体类型应用程序/pdf,web-services,rest,pdf,Web Services,Rest,Pdf,下面是RESTfulWebService代码。但是,当访问Web服务时,我会得到“未找到MIME媒体类型应用程序/pdf”。docService.findByVersionId确实返回一个“TestDoc”,它将pdf内容保存为字节[] 你能帮我解决这个问题吗 @GET @Path("/getPdf/{versionId}") @Produces("application/pdf") public Response getPdfFile(@PathParam("versi

下面是RESTfulWebService代码。但是,当访问Web服务时,我会得到“未找到MIME媒体类型应用程序/pdf”。docService.findByVersionId确实返回一个“TestDoc”,它将pdf内容保存为字节[]

你能帮我解决这个问题吗

@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;
        }

    }
错误:

 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

似乎您无法使用ByteArrayOutputStream。解决方案是使用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);
        }
    };
}

我可以在您的rest客户端上查看代码吗?或者您是否正在使用浏览器访问此pdf api?根据我的经验,在一般情况下,rest客户机(无论您使用的是什么客户机)无法反序列化响应,因为您可能忘记了提及响应的类型(在本例中为application/pdf)。在使用rest客户机代码(如果有)更新问题后,让我们进一步讨论你可能也想看看这里。这应该能回答你的问题:你找到解决方案了吗?