Reputation: 73
We have a REST API (server side) implemented using Spring Boot. This API is streaming a PDF file as StreamingResponseBody
wrapped in ResponseEntity
where content-type is given as MediaType.APPLICATION_OCTET_STREAM
.
I am trying to access this API from client application with the help of RestTemplate
. This client application is again a Spring Boot app. This client application is existing and this was supporting MappingJackson2HttpMessageConverter
with two supportive media types so far.
application/json
and application/x-www-form-urlencoded
I followed few suggestions and tried with these items
MediaType.APPLICATION_OCTET_STREAM
to existing
MappingJackson2HttpMessageConverter
ByteArrayHttpMessageConverter
which has a default support to MediaType.APPLICATION_OCTET_STREAM
ResourceHttpMessageConverter
which supporting streaming response.But with all these suggestions I was facing the following errors. At this point of time I am not really sure if is there anything that I am missing from configuration. Team, it will be of a great help really if you can redirect me to a short examples or solutions in achieving this integration.
org.springframework.web.client.RestClientException: Error while extracting response for type [interface org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody] and content type [application/octet-stream]; nested exception is org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Unexpected character ('%' (code 37)): expected a valid value (JSON String, Number, Array, Object or token 'null', 'true' or 'false'); nested exception is com.fasterxml.jackson.core.JsonParseException: Unexpected character ('%' (code 37)): expected a valid value (JSON String, Number, Array, Object or token 'null', 'true' or 'false') at [Source: (PushbackInputStream); line: 1, column: 2]
This following error was when I tried with ByteArrayHttpMessageConverter
(or) ResourceHttpMessageConverter
org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [interface org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody] and content type [application/octet-stream]
at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:123) ~[spring-web-5.2.6.RELEASE.jar!/:5.2.6.RELEASE]
Updating Question with the current implementation: This is how resttemplate bean I am creating.
@Bean
public RestTemplate restTemplate() {
final RestTemplate restTemplate = new RestTemplate(httpRequestFactory());
final List<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
final MappingJackson2HttpMessageConverter converter = new
MappingJackson2HttpMessageConverter();
final List<MediaType> mediaTypes = new ArrayList<>();
mediaTypes.add(MediaType.APPLICATION_JSON);
mediaTypes.add(MediaType.APPLICATION_FORM_URLENCODED);
//mediaTypes.add(MediaType.APPLICATION_OCTET_STREAM)
converter.setSupportedMediaTypes(mediaTypes);
messageConverters.add(converter);
restTemplate.setMessageConverters(messageConverters);
return restTemplate;
}
And my API client call is
ResponseEntity<StreamingResponseBody> response = reportRestTemplate.exchange(builder.buildAndExpand(uriParams).toUriString(),HttpMethod.GET,entity,StreamingResponseBody.class,uriParams);
Upvotes: 2
Views: 2137