Reputation: 970
I have a task to create a controller with a POST endpoint. The endpoint will take some data as an InputStream. The intent is to process that data and return a stream in the response. The response could be several megabytes in size.
Here's my controller::
@PostMapping(value = "/somePath")
public ResponseEntity<InputStream> processData(final InputStream data) {
InputStream processed = processor.process(data);
return ResponseEntity.ok().body(processed);
}
And I'm using the following to test it::
@Autowired RestTemplate restTemplate;
private ResponseEntity<InputStream> request(){
HttpHeaders headers = new HttpHeaders();
headers.setAccept(singletonList(APPLICATION_OCTET_STREAM));
ResourceHttpMessageConverter converter = new ResourceHttpMessageConverter();
converter.setSupportedMediaTypes(Arrays.asList(MediaType.APPLICATION_OCTET_STREAM));
restTemplate.getMessageConverters().add(converter);
UriComponentsBuilder builder = fromUriString(format("http://localhost:%s/%s", serverPort, "/somePath"));
UriComponents uriComponents = builder.build().encode(UTF_8);
HttpEntity<InputStream> entity = new HttpEntity<InputStream>("Hello".getBytes(), headers);
return restTemplate.exchange(uriComponents.toUri(), HttpMethod.POST, entity, InputStream.class);
}
However, I'm getting ::
org.springframework.web.client.RestClientException: No HttpMessageConverter for [B
I've tried adding a ByteArrayHttpMessageConverter to my RestTemplate, but when I do that I get::
org.springframework.web.client.HttpServerErrorException$InternalServerError: 500 : [<!doctype html><html lang="en"><head><title>HTTP Status 500 – Internal Server Error</title><style type="text/css">body {font-family:Tahoma,Arial,sans-serif;} h1, h2, h3, b {color:white;background-colo... (455 bytes)]
Any help, would be much appreciated!
Upvotes: 0
Views: 341
Reputation: 159086
You already fixed the client code by adding a ByteArrayHttpMessageConverter
, which makes sense since you're giving it a byte[]
.
You already know about the ResourceHttpMessageConverter
, which is registered by default on the server, so why not use it, i.e. give a Resource
as the payload?
Since you have an InputStream
, for which there are no message converters by default, use an InputStreamResource
:
return ResponseEntity.ok().body(new InputStreamResource(processed));
Upvotes: 1