Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/solr/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 弹簧座返回PDF-响应状态406(不可接受)_Java_Spring_Rest_Maven_Jackson - Fatal编程技术网

Java 弹簧座返回PDF-响应状态406(不可接受)

Java 弹簧座返回PDF-响应状态406(不可接受),java,spring,rest,maven,jackson,Java,Spring,Rest,Maven,Jackson,我读了很多关于这类问题的问题,但都建议使用正确的Jackson版本。这是我目前的情况: REST API: @RequestMapping(value = "get/pdf/{id}", headers="Accept=*/*", method = RequestMethod.GET, produces = "application/pdf") @Override public ResponseEntity<InputStream> getPdfContractByI

我读了很多关于这类问题的问题,但都建议使用正确的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);
我还尝试添加这两个依赖项,但没有任何变化:

    <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>
怎么了


更多细节

我正在使用此代码返回远程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;
URL=null;
试一试{
url=新url(urlStr);
}捕获(格式错误){
e、 printStackTrace();
抛出新的MyException(例如getMessage());
}
InputStream Pdfile=null;
试一试{
pdfFile=url.openStream();
}捕获(IOE异常){
e、 printStackTrace();
抛出新的MyException(例如getMessage());
}
ResponseEntity re=ResponseEntity
.ok()
//.标题(标题)
//.contentLength(contentLength)
.contentType(
parseMediaType(“application/pdf”))
.机构(pdfFile);
返回re;

基本上不需要在RequestMapping中添加
products=“application/pdf”
,因为它似乎试图在内部转换响应主体。您只需将
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);
}
@ResponseBody
@RequestMapping(value=“get/pdf/{id}”,headers=“Accept=*/*”,method=RequestMethod.get)
公共响应属性getPdfContractById(@PathVariable(“id”)长id){
//根据文件地址获取删除文件
RemoteFile RemoteFile=新的RemoteFile(id);
//设置输入流
InputStream InputStream=remotefile.getInputStream();
//告诉我那是一个PDF文件
HttpHeaders responseHeaders=新的HttpHeaders();
InputStreamResource InputStreamResource=新的InputStreamResource(inputStream);
responseHeaders.setContentLength(contentLengthOfStream);
setContentType(MediaType.valueOf(“application/pdf”);
//以防您需要支持浏览器
responseHeaders.put(“内容处置”,Collections.singletonList(“附件;filename=somefile.pdf”))
返回新的响应属性(inputStreamResource,
负责人,
HttpStatus.OK);
}

为什么类路径上同时有Jackson 1和Jackson 2?根据这一点:我应该拥有所有这些依赖项。对不对?删除两个依赖项(版本1.9.13)不会改变响应。(这不是为了解决您的问题。)那篇文章完全是胡说八道。你不需要两者兼而有之。它们都使Spring提供一个处理JSON的
HttpMessageConverter
。你只需要一个。好吧,我把我的帖子写得更精确一点。你说的很有道理,这是我以前的配置。谢谢你的回答。我更新了我的OP并添加了我正在使用的代码。它非常相似,但我不能使用InputStreamResource(我的输入流已经被读取)。不过,这似乎不起作用。您是否已从RequestMapping中删除了
products
?您的流在哪里读取?我不明白为什么不能使用InputStream Reader,但如果可以,请尝试在Response中返回一个字节[],看看它是否有效……因此,让我们尝试一下我建议的方法,删除products,如果不能在Response实体中返回字节[]。太好了!返回字节数组解决了这个问题。另外,我没有在RequestMapping注释中使用“products”,也不需要添加Jackson依赖项。只需添加(“Content Disposition”、“attachment;filename=somefile.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;
@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);
}