java Spring REST 返回 PDF - 响应状态 406(不可接受)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33876635/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring REST returning PDF - Response status 406 (not acceptable)
提问by Manu
I read many questions on SO about this type of issue, but all of them recommend using the correct Hymanson version. This is my current situation:
我在 SO 上阅读了许多关于此类问题的问题,但所有这些问题都建议使用正确的 Hymanson 版本。这是我目前的情况:
REST API:
REST API:
@RequestMapping(value = "get/pdf/{id}", headers="Accept=*/*", method = RequestMethod.GET, produces = "application/pdf")
@Override
public ResponseEntity<InputStream> getPdfContractById(@PathVariable("id") Long id);
Using Accept:*/*
produces an error in mapping the request (404 occurs)
使用Accept:*/*
在映射请求时产生错误(发生 404)
From my pom:
从我的pom:
<dependency>
<groupId>com.fasterxml.Hymanson.core</groupId>
<artifactId>Hymanson-core</artifactId>
<version>2.4.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.Hymanson.core</groupId>
<artifactId>Hymanson-databind</artifactId>
<version>2.4.1.1</version>
</dependency>
I also tried to add these two dependencies, but nothing changes:
我也尝试添加这两个依赖项,但没有任何变化:
<dependency>
<groupId>org.codehaus.Hymanson</groupId>
<artifactId>Hymanson-core-asl</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>org.codehaus.Hymanson</groupId>
<artifactId>Hymanson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
Response client-side: There was an unexpected error (type=Not Acceptable, status=406).
Headers incude:
响应客户端:标There was an unexpected error (type=Not Acceptable, status=406).
头包括:
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip, deflate, sdch
What's wrong with it?
它出什么问题了?
More details更多细节
I am using this code to return the remote PDF file:
我正在使用此代码返回远程 PDF 文件:
URL url = null;
try {
url = new URL(urlStr);
} catch (MalformedURLException e) {
e.printStackTrace();
throw new MyException(e.getMessage());
}
InputStream pdfFile = null;
try {
pdfFile = url.openStream();
} catch (IOException e) {
e.printStackTrace();
throw new MyException(e.getMessage());
}
ResponseEntity<InputStream> re = ResponseEntity
.ok()
// .headers(headers)
// .contentLength(contentLength)
.contentType(
MediaType.parseMediaType("application/pdf"))
.body(pdfFile);
return re;
回答by Babl
Basically there is no need to add produces = "application/pdf"
in RequestMapping as it seems to try to convert the ResponeBody internally. You can just add MediaType
to response headers which is what you need.
基本上没有必要添加produces = "application/pdf"
RequestMapping 因为它似乎试图在内部转换 ResponeBody。您只需添加MediaType
到您需要的响应标头即可。
@ResponseBody
@RequestMapping(value = "get/pdf/{id}", headers="Accept=*/*", method = RequestMethod.GET)
public ResponseEntity<InputStreamResource> getPdfContractById(@PathVariable("id") Long id){
// Get the remove file based on the fileaddress
RemoteFile remotefile = new RemoteFile(id);
// Set the input stream
InputStream inputstream = remotefile.getInputStream();
// asume that it was a PDF file
HttpHeaders responseHeaders = new HttpHeaders();
InputStreamResource inputStreamResource = new InputStreamResource(inputStream);
responseHeaders.setContentLength(contentLengthOfStream);
responseHeaders.setContentType(MediaType.valueOf("application/pdf"));
// just in case you need to support browsers
responseHeaders.put("Content-Disposition", Collections.singletonList("attachment; filename=somefile.pdf"))
return new ResponseEntity<InputStreamResource> (inputStreamResource,
responseHeaders,
HttpStatus.OK);
}