我读了很多关于此类问题的问题,但他们都建议使用正确的杰克逊版本。这是我目前的情况:
休息 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:*/*
映射请求时产生错误(发生 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>
响应客户端: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
它出什么问题了?
More details
我使用此代码返回远程 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;