java – Spring REST返回PDF – 响应状态406(不可接受)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – Spring REST返回PDF – 响应状态406(不可接受),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2555字,纯文字阅读大概需要4分钟。
内容图文
![java – Spring REST返回PDF – 响应状态406(不可接受)](/upload/InfoBanner/zyjiaocheng/810/96fb293484754f29b7d8bb96afb6ebee.jpg)
关于这类问题,我读了很多关于SO的问题,但是他们都建议使用正确的Jackson版本.这是我目前的情况:
REST API:
@RequestMapping(value = "get/pdf/{id}", headers="Accept=*/*", method = RequestMethod.GET, produces = "application/pdf")
@Override
public ResponseEntity<InputStream> getPdfContractById(@PathVariable("id") Long id);
使用Accept:* / *在映射请求时产生错误(404发生)
从我的pom:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.4.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.4.1.1</version>
</dependency>
我也尝试添加这两个依赖项,但没有任何变化:
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
响应客户端:出现意外错误(type = Not Acceptable,status = 406).Headers包含:
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip, deflate, sdch
它出什么问题了?
更多细节
我使用此代码返回远程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;
解决方法:
基本上没有必要在RequestMapping中添加produce =“application / pdf”,因为它似乎试图在内部转换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"));
return new ResponseEntity<InputStreamResource> (inputStreamResource,
responseHeaders,
HttpStatus.OK);
}
内容总结
以上是互联网集市为您收集整理的java – Spring REST返回PDF – 响应状态406(不可接受)全部内容,希望文章能够帮你解决java – Spring REST返回PDF – 响应状态406(不可接受)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。