Skip to content

Instantly share code, notes, and snippets.

@azhawkes
Created August 19, 2015 17:26
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save azhawkes/f714e46b7e25233b1668 to your computer and use it in GitHub Desktop.
Save azhawkes/f714e46b7e25233b1668 to your computer and use it in GitHub Desktop.
Spring Boot (RestController) - support for application/octet-stream using InputStream
/**
* Adds support for application/octet-stream through a RestController using streams.
*/
@Configuration
class WebConfig extends WebMvcConfigurationSupport {
@Override
protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(new AbstractHttpMessageConverter<InputStream>(MediaType.APPLICATION_OCTET_STREAM) {
protected boolean supports(Class<?> clazz) {
return InputStream.isAssignableFrom(clazz)
}
protected InputStream readInternal(Class<? extends InputStream> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
return inputMessage.body
}
protected void writeInternal(InputStream inputStream, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
IOUtils.copy(inputStream, outputMessage.body)
}
})
super.configureMessageConverters(converters);
}
}
@manojsa
Copy link

manojsa commented Oct 5, 2017

Thanks. Very helpful...!!!

@benoitdevos
Copy link

Thanks for this, really useful. Note that I made a Java version from the groovy one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment