2014-01-02 1 views
0

У меня есть это ниже спокойного кода webservice. Но при доступе к веб-сервису я получаю «MIME media type application/pdf не найден». DocService.findByVersionId возвращает «TestDoc», который содержит PDF-содержимое в качестве байта [].Приложение типа мультимедиа IME/pdf не найдено в restful webservice

Не могли бы вы помочь мне в решении этой проблемы?

@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 
+0

Могу ли я увидеть код на вашем клиенте отдыха или использовать браузер для доступа к этому pdf-апитуму? В общих условиях, по моему опыту, клиент-клиент (любой клиент, которого вы могли использовать) не может десериализовать ответ, потому что вы, вероятно, забыли указать ТИП ответа (в данном случае приложение/pdf). Давайте обсудим больше, как только вы обновите вопрос своим кодом клиента-клиента (если есть) – shahshi15

+0

Возможно, вы захотите посмотреть здесь. Это должно ответить на ваш вопрос: http://stackoverflow.com/a/3503704/3066911 – shahshi15

+0

Вы нашли решение? – ronnyfm

ответ

1

кажется, что вы не можете использовать 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); 
     } 
    }; 
} 
Смежные вопросы