Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Spring Boot (RestController) - support for application/octet-stream using InputStream
* Adds support for application/octet-stream through a RestController using streams.
class WebConfig extends WebMvcConfigurationSupport {
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)
Copy link

azhawkes commented Aug 19, 2015

I was using RestController and wanted to have certain endpoints that accept binary file uploads with Content-Type: application/octet-stream, and certain other endpoints that deliver downloads the same way. I did not want to use multipart/form-data.

As far as I could tell, Spring does not support this out of the box, but it's easy enough to add as shown in this gist.

Once the message converter is registered, here's an example of it in use:

class RepositoryController {

    @ApiOperation(value = "Store an asset")
    @RequestMapping(value = "/api/repository/assets/{id}", method = RequestMethod.POST, consumes = "application/octet-stream")
    void storeAsset(@PathVariable String id, RequestEntity<InputStream> entity) {
        repositoryService.saveAsset(id, entity.getBody())

Copy link

MichaelF25 commented Jun 17, 2016

Perfect - I was not aware that spring is not able for such a simple thing... thx a lot!

Copy link

kajuwise commented Mar 18, 2017

Lovely trick

Copy link

manojsa commented Oct 5, 2017

Thanks. Very helpful...!!!

Copy link

benoitdevos commented Oct 30, 2017

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