Spring REST返回PDF-响应状态406(不可接受) [英] Spring REST returning PDF - Response status 406 (not acceptable)

查看:422
本文介绍了Spring REST返回PDF-响应状态406(不可接受)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于此类问题,我在SO上阅读了许多问题,但所有这些问题都建议使用正确的Jackson版本.这是我目前的情况:

I read many questions on SO about this type of issue, but all of them recommend using the correct Jackson version. This is my current situation:

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)

Using Accept:*/* produces an error in mapping the request (404 occurs)

从我的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>

我还尝试添加这两个依赖项,但没有任何变化:

I also tried to add these two dependencies, but nothing changes:

    <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).标头包括:

Response client-side: There was an unexpected error (type=Not Acceptable, status=406).Headers incude:

    Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip, deflate, sdch

怎么了?


更多详细信息

我正在使用以下代码返回远程PDF文件:

I am using this code to return the remote PDF file:

    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中添加produces = "application/pdf",因为它似乎尝试在内部转换ResponeBody.您只需将MediaType添加到所需的响应标题中即可.

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.

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

这篇关于Spring REST返回PDF-响应状态406(不可接受)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆