0

I want to handle the POST request when there is empty content-type.

  1. When I add consumes = MediaType.APPLICATION_JSON_VALUE and make a request in postman with Content-type blank I get the following error
{
    "timestamp": 1581594986909,
    "status": 415,
    "error": "Unsupported Media Type",
    "message": "Content type '' not supported",
    "path": "/test"
}

Here is the code


  @PostMapping(produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity create(@RequestBody TestRequest testRequest) throws TestException {
        LOG.debug("Starting...");
        //code
        return createtest(testRequest);
    }
  1. when i remove consumes = MediaType.APPLICATION_JSON_VALUE and make a request with content-type = blank i get the following error
{
    "timestamp": 1581595348209,
    "status": 415,
    "error": "Unsupported Media Type",
    "message": "Content type 'application/octet-stream' not supported",
    "path": "/test"
}

Here is the code

@PostMapping(produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity create(@RequestBody TestRequest testRequest) throws TestException {
        LOG.debug("Starting...");
        //code
        return createtest(testRequest);
    }

Here is the POstMan request enter image description here

I want to handle this scenario and assume as if content-Type= application/json is sent

2 Answers2

0

To handle empty Content-Type as if application/json, you need to configure MappingJackson2HttpMessageConverter to support application/octet-stream and controller's method (i.e. your create method) consumes both application/octet-stream and application/json.

For example:

[CONFIGURATION]

@Configuration(proxyBeanMethods = false)
public class MyConfigurer {

    @Bean
    public HttpMessageConverters customConverters() {
        Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(builder.build());
        List<MediaType> supportedMediaTypes = new ArrayList<>();
        supportedMediaTypes.addAll(converter.getSupportedMediaTypes());
        supportedMediaTypes.add(MediaType.APPLICATION_OCTET_STREAM);
        converter.setSupportedMediaTypes(supportedMediaTypes);
        return new HttpMessageConverters(converter);
    }

}

[CONTROLLER'S METHOD]

@PostMapping(produces = MediaType.APPLICATION_JSON_VALUE, consumes = {
            MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_OCTET_STREAM_VALUE })
public ResponseEntity create(@RequestBody TestRequest testRequest) throws TestException {
        LOG.debug("Starting...");
        //code
        return createtest(testRequest);
}

Spring seems to assume that Content-Type is application/octet-stream when Content-Type is empty and by default configuration MappingJackson2HttpMessageConverter supports application/json and application/*+json only. Therefore you need to modify your configuration and controller's method like above.

Following references are helpful for you:
Javadoc of MappingJackson2HttpMessageConverter
Spring boot documents

Tomoki Sato
  • 578
  • 4
  • 11
  • I am still getting 415 Unsupported media even though I can see that Bean is configured properly and has three supported media value. When i pass content-Type = blank it still is not working. –  Feb 14 '20 at 09:09
  • Sorry I can't recognize the exact cause of your `415` error but still I can come up with 2 possible causes. 1) Your `MappingJackson2HttpMessageConverter ` still fails to handle `application/octet-stream`. You can clarify the cause by debugging Spring's `AbstractMessageConverterMethodArgumentResolver#readWithMessageConverters`(if possible). 2) Your controller's method mapped to the request doesn't consume `MediaType.APPLICATION_OCTET_STREAM_VALUE`. For further investigation, please show your spring boot version, source code of `MappingJackson2HttpMessageConverter ` configuration. – Tomoki Sato Feb 14 '20 at 13:09
0

I finally configured it and it is working. Here is the correct configuration for MappingJackson2HttpMessageConverter


@Configuration(proxyBeanMethods = false)
public class WebMvcConfig implements WebMvcConfigurer {

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(jacksonMessageConverter());
        WebMvcConfigurer.super.configureMessageConverters(converters);
    }

    @Bean
    public MappingJackson2HttpMessageConverter jacksonMessageConverter() {
        MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter();

        List<MediaType> supportedMediaTypes=new ArrayList<>();
        supportedMediaTypes.addAll(messageConverter.getSupportedMediaTypes());
        messageConverter.setSupportedMediaTypes(supportedMediaTypes);
        supportedMediaTypes.add(MediaType.APPLICATION_OCTET_STREAM);
        messageConverter.setSupportedMediaTypes(supportedMediaTypes);
        messageConverter.setPrettyPrint(true);
        return messageConverter;
    }

Aso add the APPLICATION_OCTET_STREAM_VALUE } in the controller method you want to support the octet-stream.

consumes = { MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_OCTET_STREAM_VALUE
 }