使用 restTemplate 下载内容类型八位字节流的文件
Download file of content-type octet-stream using restTemplate
在我的应用程序中,我收到了来自 API 响应的文件,其 Content-Type
是 application/octet-stream
。
我正在尝试 return 这个文件作为 ResponseEntity<Resource>
到前端。
但我收到错误消息:
RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [class org.springframework.http.ResponseEntity] and content type [application/octet-stream]
通过浏览一些文档,我想到使用 responseEntityExtractor
,与 RestTemplate.getForEntity 中的相同。
我的代码:
RequestCallback requestCallback = request -> {
HttpHeaders headers1 = request
.getHeaders();
headers1.set("Authorization", "Bearer " + template.getAccessToken());
headers1
.setAccept(Arrays.asList(MediaType.APPLICATION_OCTET_STREAM, MediaType.ALL));
};
ResponseExtractor<ResponseEntity<Resource>> responseExtractor = template.responseEntityExtractor(ResponseEntity.class);
template.execute(url, HttpMethod.GET, requestCallback, responseExtractor, uriVariables);
可以理解的是,通过在请求回调中设置 setAccept
我们让 restTemplate
知道期望的内容类型,而 responseEntityExtractor()
将 return ResponseExtractor<ResponseEntity<T>>
.
我错过了什么?
而不是:
ResponseExtractor<ResponseEntity<Resource>> responseExtractor = template.responseEntityExtractor(ResponseEntity.class);
尝试在控制器方法中 return byte[]
:
ResponseExtractor<ResponseEntity<byte[]>> responseExtractor = template.responseEntityExtractor(byte[].class);
ResponseEntity<byte[]> results = template.execute(url, HttpMethod.GET, requestCallback, responseExtractor, uriVariables);
return results.getBody();
或此为 return ResponseEntity<Resource>
:
ResponseExtractor<ResponseEntity<byte[]>> responseExtractor = template.responseEntityExtractor(byte[].class);
ResponseEntity<byte[]> results = template.execute(url, HttpMethod.GET, requestCallback, responseExtractor, uriVariables);
ByteArrayResource byteArrayResource = new ByteArrayResource(results.getBody());
return new ResponseEntity<Resource>(byteArrayResource, null, HttpStatus.OK);
在我的应用程序中,我收到了来自 API 响应的文件,其 Content-Type
是 application/octet-stream
。
我正在尝试 return 这个文件作为 ResponseEntity<Resource>
到前端。
但我收到错误消息:
RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [class org.springframework.http.ResponseEntity] and content type [application/octet-stream]
通过浏览一些文档,我想到使用 responseEntityExtractor
,与 RestTemplate.getForEntity 中的相同。
我的代码:
RequestCallback requestCallback = request -> {
HttpHeaders headers1 = request
.getHeaders();
headers1.set("Authorization", "Bearer " + template.getAccessToken());
headers1
.setAccept(Arrays.asList(MediaType.APPLICATION_OCTET_STREAM, MediaType.ALL));
};
ResponseExtractor<ResponseEntity<Resource>> responseExtractor = template.responseEntityExtractor(ResponseEntity.class);
template.execute(url, HttpMethod.GET, requestCallback, responseExtractor, uriVariables);
可以理解的是,通过在请求回调中设置 setAccept
我们让 restTemplate
知道期望的内容类型,而 responseEntityExtractor()
将 return ResponseExtractor<ResponseEntity<T>>
.
我错过了什么?
而不是:
ResponseExtractor<ResponseEntity<Resource>> responseExtractor = template.responseEntityExtractor(ResponseEntity.class);
尝试在控制器方法中 return byte[]
:
ResponseExtractor<ResponseEntity<byte[]>> responseExtractor = template.responseEntityExtractor(byte[].class);
ResponseEntity<byte[]> results = template.execute(url, HttpMethod.GET, requestCallback, responseExtractor, uriVariables);
return results.getBody();
或此为 return ResponseEntity<Resource>
:
ResponseExtractor<ResponseEntity<byte[]>> responseExtractor = template.responseEntityExtractor(byte[].class);
ResponseEntity<byte[]> results = template.execute(url, HttpMethod.GET, requestCallback, responseExtractor, uriVariables);
ByteArrayResource byteArrayResource = new ByteArrayResource(results.getBody());
return new ResponseEntity<Resource>(byteArrayResource, null, HttpStatus.OK);