RestTemplate

RestTemplate 调用返回InputStreamResource方法

converter.HttpMessageNotReadableException: Unsupported resource class: class org.springframework.core.io.InputStreamResource
被调用方:`
HttpHeaders headers = new HttpHeaders();
headers.add(“Cache-Control”, “no-cache, no-store, must-revalidate”);
headers.add(“Content-Disposition”, String.format(“attachment; filename=”%s"", file.getFilename()));
headers.add(“Pragma”, “no-cache”);
headers.add(“Expires”, “0”);

            return ResponseEntity
                    .ok()
                    .headers(headers)
                    .contentLength(file.contentLength())
                    .contentType(MediaType.parseMediaType("application/octet-stream"))
                    .body(new InputStreamResource(file.getInputStream()));`

调用方`
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON); //make JSON parameters

    //make back to no matter what type on octet-stream
    List<MediaType> mediaTypeList = new ArrayList<>();
    mediaTypeList.add(MediaType.parseMediaType("application/octet-stream"));
    headers.setAccept(mediaTypeList);

    HttpEntity<String> entity = new HttpEntity<>(JSON.toJSONString(request), headers);
    //main aim to InputStreamResource transform Resource(important)
    ResponseEntity<InputStreamResource> postForEntity = restTemplate.exchange(sb.toString(), HttpMethod.POST, entity, InputStreamResource.class);`

设置返回类型:application/octet-stream
原因:RestTemplate对InputStreamResource不支持,可修改为返回类型为接口接收:Resource.class

       HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON); //make JSON parameters

        //make back to no matter what type on octet-stream
        List<MediaType> mediaTypeList = new ArrayList<>();
        mediaTypeList.add(MediaType.parseMediaType("application/octet-stream"));
        headers.setAccept(mediaTypeList);

        HttpEntity<String> entity = new HttpEntity<>(JSON.toJSONString(request), headers);
        //main aim to InputStreamResource transform Resource(important)
        ResponseEntity<Resource> postForEntity = restTemplate.exchange(sb.toString(), HttpMethod.POST, entity, Resource.class);
发布了3 篇原创文章 · 获赞 2 · 访问量 333
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览