Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Adding support for application/octet-stream in RestController (Spring Boot) - java version
package mycompany.myappp.config;
import org.apache.commons.io.IOUtils;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.MediaType;
import org.springframework.http.converter.AbstractHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
/**
* Configure Spring Boot to allow upload of octet-stream.
*/
@Configuration
public 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.class.isAssignableFrom(clazz);
}
protected InputStream readInternal(Class<? extends InputStream> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
return inputMessage.getBody();
}
protected void writeInternal(InputStream inputStream, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
IOUtils.copy(inputStream, outputMessage.getBody());
}
});
super.configureMessageConverters(converters);
}
}
@benoitdevos
Copy link
Author

benoitdevos commented Oct 30, 2017

This is the java version of WebConfig.groovy. Many thanks to @azhawkes

@johannesjasper
Copy link

johannesjasper commented Nov 28, 2017

@benoitdevos Thanks for the conversion!
While your code worked out of the box, I had problems with my existing jackson converter.
The following variant works well in my Spring Boot (!) application using @Bean:

@Bean
public HttpMessageConverter addOctetStreamConverter() {
    return new AbstractHttpMessageConverter<InputStream>(MediaType.APPLICATION_OCTET_STREAM) {
        protected boolean supports(Class<?> clazz) {
            return InputStream.class.isAssignableFrom(clazz);
        }

        protected InputStream readInternal(Class<? extends InputStream> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
            return inputMessage.getBody();
        }

        protected void writeInternal(InputStream inputStream, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
            IOUtils.copy(inputStream, outputMessage.getBody());
        }
    };
}

@phil294
Copy link

phil294 commented Apr 29, 2018

... or simply adding octet to the default jackson converter:

@Configuration
public class WebConfig extends WebMvcConfigurationSupport {
    @Override
    protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        converter.setSupportedMediaTypes(Arrays.asList(
            MediaType.APPLICATION_JSON,
            new MediaType("application", "*+json"),
            MediaType.APPLICATION_OCTET_STREAM
        ));
        converters.add(converter);
        super.configureMessageConverters(converters);
    }
}

@jmaerki
Copy link

jmaerki commented Nov 13, 2019

phil294's approach was killing static resources for me (404s), but this worked for me:

@Configuration
public class JacksonConfiguration {
    @Bean
    MappingJackson2HttpMessageConverter customizedJacksonMessageConverter() {
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        converter.setSupportedMediaTypes(
                Arrays.asList(
                        MediaType.APPLICATION_JSON,
                        new MediaType("application", "*+json"),
                        MediaType.APPLICATION_OCTET_STREAM));
        return converter;
    }
}

@mingrn97
Copy link

mingrn97 commented Nov 18, 2021

Great :)

@cpeeyush
Copy link

cpeeyush commented Feb 15, 2022

Nice work!

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