Spring REST 返回 PDF - 响应状态 406 (不可接受)

8
我在 Stack Overflow 上看到很多关于这种问题的提问,但是所有的建议都是使用正确的 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>

客户端响应:发生了意外的错误(类型=不可接受,状态=406)。标头包括:

    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;

1
你的类路径上为什么同时存在Jackson 1和Jackson 2? - Sotirios Delimanolis
根据这个链接:https://dev59.com/NWs05IYBdhLWcg3wFN8h#26616553,我应该拥有所有这些依赖项。这不正确吗?删除这两个依赖项(版本1.9.13)并没有改变响应。 - Manu
1
(这不是为了解决你的问题。)那篇帖子完全是胡说八道。你不需要两个。它们中的每一个都会导致Spring提供一个处理JSON的HttpMessageConverter。你只需要一个。 - Sotirios Delimanolis
好的,我会让我的帖子更加精确。你说的实际上很有道理,这是我的先前配置。 - Manu
1个回答

11
基本上在RequestMapping中不需要添加"produces = "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"));
        // 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);
}

谢谢你的回答。我更新了我的OP并添加了我正在使用的代码。它非常相似,但我不能使用InputStreamResource(我的输入流已经被读取)。尽管如此,那似乎也不起作用。 - Manu
你从RequestMapping中移除了produces吗?你的流在哪里读取?我不明白为什么你不能使用InputStream Reader,但如果可以的话,尝试在响应中返回一个byte[],看看是否有效... - Babl
1
那么让我们尝试一下我建议的事情,删除 produces,并且如果不是,则在响应实体中返回 byte[]。 - Babl
1
太好了!返回字节数组解决了问题。此外,我在RequestMapping注释中没有使用“produces”,也不需要添加Jackson依赖项。 - Manu
2
只需添加 ("Content-Disposition", "attachment; filename=somefile.pdf") 标头。 - Babl
显示剩余4条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接