Skip to content

Instantly share code, notes, and snippets.

@benoitdevos
Last active March 19, 2022 19:29
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save benoitdevos/fc49f3b9633eb7ba0f5c8dfe085cba14 to your computer and use it in GitHub Desktop.
Save benoitdevos/fc49f3b9633eb7ba0f5c8dfe085cba14 to your computer and use it in GitHub Desktop.
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

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

@johannesjasper
Copy link

@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;
    }
}

@magicianlib
Copy link

Great :)

@cpeeyush
Copy link

Nice work!

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