2017-12-15 4 views
1

봄과 함께 나머지 API를 호출하는 PDF 파일을 다운로드해야합니다. 내 코드는 다음iText를 사용하여 봄과 편안한 서비스에서 PDF를 다운로드

@RequestMapping(value = "downloadPDF", method = RequestMethod.GET, produces = MediaType.APPLICATION_PDF_VALUE) 
public @ResponseBody ResponseEntity<InputStreamResource> getPDF() throws IOException { 

    ByteArrayInputStream bis = GeneratePdfReport.createReport(); 

    HttpHeaders headers = new HttpHeaders(); 
    headers.add("Content-Disposition", "inline; filename=migration.pdf"); 


    return ResponseEntity 
      .ok() 
      .headers(headers) 
      .contentType(MediaType.APPLICATION_PDF) 
      .body(new InputStreamResource(bis)); 

} 

이이 문서에 간단한 표를 인쇄 createReport의 방법을 나는 나머지 API를 호출하고 나는이 오류를 얻을 때 문제가 발생합니다

public static ByteArrayInputStream createReport() { 


    Document document = new Document(); 
    ByteArrayOutputStream out = new ByteArrayOutputStream(); 

    try { 

     PdfPTable table = new PdfPTable(3); 
     table.setWidthPercentage(60); 
     table.setWidths(new int[]{1, 3, 3}); 

     Font headFont = FontFactory.getFont(FontFactory.HELVETICA_BOLD); 

     PdfPCell hcell; 
     hcell = new PdfPCell(new Phrase("Id", headFont)); 
     hcell.setHorizontalAlignment(Element.ALIGN_CENTER); 
     table.addCell(hcell); 

     hcell = new PdfPCell(new Phrase("Name", headFont)); 
     hcell.setHorizontalAlignment(Element.ALIGN_CENTER); 
     table.addCell(hcell); 

     hcell = new PdfPCell(new Phrase("Population", headFont)); 
     hcell.setHorizontalAlignment(Element.ALIGN_CENTER); 
     table.addCell(hcell); 


     PdfWriter.getInstance(document, out); 
     document.open(); 
     document.add(table); 

     document.close(); 

    } catch (DocumentException ex) { 

     System.out.println("error"); 
    } 

    return new ByteArrayInputStream(out.toByteArray()); 
} 

서버

HTTP Status 406 – Not Acceptable 

The target resource does not have a current representation that would be acceptable to the user agent, according to the proactive negotiation header fields received in the request, and the server is unwilling to supply a default representation 
+0

내가 https://stackoverflow.com/questions/45159523/download-file-using-spring-restful로 해결을 -service – chris

답변

0

서비스의 406 응답 유형은 응답 유형 서비스가 반환하는 것이 클라이언트 요청의 HTTP 헤더 승인에 제공되지 않음을 의미합니다.

서비스가 MediaType.APPLICATION_PDF_VALUE 응답 유형을 생성하므로 HttpMessageConverter를 찾습니다. Spring은 오류 응답을 application/pdf로 변환하려고 시도하지만 PDF 로의 변환을 지원하는 적합한 HttpMessageConverter를 찾지 못한다.

그래서 다음 예와 같이 RestTemplate에 MessageConvertor을 추가해야

ByteArrayHttpMessageConverter byteArrayHttpMessageConverter = new ByteArrayHttpMessageConverter(); 

List<MediaType> supportedApplicationTypes = new ArrayList<MediaType>(); 
MediaType pdfApplication = new MediaType("application","pdf"); 
supportedApplicationTypes.add(pdfApplication); 

byteArrayHttpMessageConverter.setSupportedMediaTypes(supportedApplicationTypes); 
List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>(); 
messageConverters.add(byteArrayHttpMessageConverter); 
restTemplate = new RestTemplate(); 
restTemplate.setMessageConverters(messageConverters); 

Object result = getRestTemplate().getForObject(url, returnClass, parameters); 
byte[] resultByteArr = (byte[])result; 
+0

'headers.add ("Accept", "application/pdf");를 추가하려고했지만 동일한 오류가 있습니다. – chris

+0

java.lang.IllegalStateException 제거 :이 응답에 대해 getOutputStream()이 이미 호출되었습니다. 예외가 java.lang.IllegalStateException 인 경우 : getOutputStream()이이 응답에 대해 이미 호출되었습니다. ' – chris

관련 문제