简体   繁体   English

生产 = "application/pdf" 不适用于 Spring Boot

[英]produces = "application/pdf" not working on Spring Boot

I have Spring Boot project and I am using Jasper Report.我有 Spring Boot 项目,我正在使用 Jasper Report。 I posted some json data and response return to me this:我发布了一些 json 数据,响应返回给我:

%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 生成 = "application/pdf" 但它不起作用。

@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:我的prepareResponse方法:

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

Adding code produces = {"application/pdf"} works for me.添加代码produces = {"application/pdf"}对我有用。 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);
}

Just to bring this thread up, if someone is reaching here when getting an error that no suitable converter is found for application/pdf .只是为了提出这个线程,如果有人在收到错误时到达这里,即没有找到适用于application/pdf合适转换器。
Check if you are using the @RepositoryRestController annotation.检查您是否正在使用@RepositoryRestController注释。
If that is the case, try to switch to @RestController .如果是这种情况,请尝试切换到@RestController

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM