Reputation: 1571
I have Spring Boot project and I am using Jasper Report. I posted some json data and response return to me this:
%PDF-1.5
%����
1 0 obj
<undefined</Filter/FlateDecode/Length 29>>stream
x�+�r
�26S�00SI�r
�
��13-
endstream
endobj
3 0 obj<undefined</Tabs/S/Group<undefined</S/Transparency/Type/Group/CS/DeviceRGB>>/Contents 1 0 R/Type/Page/Resources<undefined</ColorSpace<</CS/DeviceRGB>>/ProcSet [/PDF /Text /ImageB /ImageC /ImageI]>>/Parent 2 0 R/MediaBox[0 0 595 842]>>
endobj
2 0 obj
<undefined</Kids[3 0 R]/Type/Pages/Count 1/ITXT(2.1.7)>>
endobj
4 0 obj<undefined</Type/Catalog/Pages 2 0 R/ViewerPreferences<undefined</PrintScaling/AppDefault>>>>
endobj
5 0 obj<undefined</ModDate(D:20160710203902+05'00')/Creator(JasperReports Library version 6.2.0)/CreationDate(D:20160710203902+05'00')/Producer(iText 2.1.7 by 1T3XT)>>
endobj
xref
0 6
0000000000 65535 f
0000000015 00000 n
0000000333 00000 n
0000000110 00000 n
0000000396 00000 n
0000000487 00000 n
trailer<undefined</Info 5 0 R/ID [undefined<07942c7c1b5b6068753ddc445ec60abf>undefined<c82bba08c068c3699915ac33668fef92>]/Root 4 0 R/Size 6>>
startxref
654
%%EOF
On my rest controller I added to RequestMapping produces = "application/pdf" but it is not working.
@RequestMapping(value = "/gMapReports", method = RequestMethod.POST, produces = "application/pdf")
public ResponseEntity<InputStreamResource> gMapReports(@RequestBody GMapReportRequest gMapReportRequest) {
return reportService.prepareResponse(reportService.gMapReports(gMapReportRequest));
}
My prepareResponse method:
public ResponseEntity<InputStreamResource> prepareResponse(File reportDocument) {
FileInputStream fileInputStream = fileStreamConverter.getFileInputStream(reportDocument);
return ResponseEntity
.ok()
.contentLength(reportDocument.length())
.contentType(MediaType.APPLICATION_OCTET_STREAM)
.header("content-disposition", "filename=report")
.body(new InputStreamResource(fileInputStream));
}
Upvotes: 2
Views: 8394
Reputation: 561
Just to bring this thread up, if someone is reaching here when getting an error that no suitable converter is found for application/pdf
.
Check if you are using the @RepositoryRestController
annotation.
If that is the case, try to switch to @RestController
.
Upvotes: 0
Reputation: 8598
Adding code produces = {"application/pdf"}
works for me. Full example:
@RequestMapping(value = "/pdfFile", method = RequestMethod.POST, produces = {"application/pdf"})
@ResponseBody
public FileSystemResource getFile(@ModelAttribute PdfFile pdfFile) {
PdfFileGenerator pdfFileGenerator = new PdfFileGeneratorImpl();
File file = pdfFileGenerator.generatePdf(pdfFile);
return new FileSystemResource(file);
}
Upvotes: 3