2

I'm trying to add msgpack binary dataformat as a content negotiation option. The Json and Xml works fine out of the box. I tried to add jackson msgpack mapper as a bean, like in this examle, but it does not work. When I add Accept: application/x-msgpack header to my request 406 Not Acceptable code is returned.

Here's my WebConfig:

@Configuration
@EnableWebMvc
@SuppressWarnings("unused")
public class WebConfig extends WebMvcConfigurerAdapter {

    @Bean
    public HttpMessageConverter messagePackMessageConverter() {
        return new AbstractJackson2HttpMessageConverter(
                new ObjectMapper(new MessagePackFactory()),
                new MediaType("application", "x-msgpack")) {
        };
    }

    @Override
    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
        configurer.favorPathExtension(false)
                .ignoreAcceptHeader(false)
                .favorParameter(true)
                .defaultContentType(MediaType.APPLICATION_JSON)
                .mediaType("xml", MediaType.APPLICATION_XML)
                .mediaType("msgpack", new MediaType("application", "x-msgpack"));
    }

}

I didn't add any special annotations to my DTO objects, and my controller is nothing out of the ordinary either.

My msgpack dependency is:

org.msgpack:jackson-dataformat-msgpack:0.7.0-p3
Ben
  • 3,989
  • 9
  • 48
  • 84

2 Answers2

1

Apparently bean injection did not work (I would be glad if someone showed me how to auto inject a new HttpMessageConverter). So I added it manually:

public class WebConfig extends WebMvcConfigurerAdapter {


    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(messagePackMessageConverter());
        converters.add(new MappingJackson2HttpMessageConverter());
        converters.add(new MappingJackson2XmlHttpMessageConverter());

        super.configureMessageConverters(converters);
    }

    //...
Ben
  • 3,989
  • 9
  • 48
  • 84
0

Can be done in 2 ways -

  1. With spring boot just creating a @Bean of type HttpMessageConverter should do the job. spring boot will add it automatically to the list of message converters. (But doing this also means if the existing application was returning "application/json" as default response when Accept header was not supplied then it will now return default "application/x-msgpack")

     @Bean    
     public HttpMessageConverter messagePackMessageConverter() {
             MediaType msgPackMediaType = new MediaType("application", "x-msgpack");
             return new AbstractJackson2HttpMessageConverter(new ObjectMapper(new 
                  MessagePackFactory()), msgPackMediaType) {
             };
     }
    
  2. this solution is similar to the above solution of using WebMvcConfigurerAdapter but instead of using configureMessageConverters I would suggest using extendMessageConverters , since using configureMessageConverters would turn off default converter registration done by spring. Usage example -

     @Configuration
     class WebConfig implements WebMvcConfigurer {    
    
       HttpMessageConverter messagePackMessageConverter() {
             MediaType msgPackMediaType = new MediaType("application", "x-msgpack");
             return new AbstractJackson2HttpMessageConverter( new ObjectMapper( new 
                MessagePackFactory()), msgPackMediaType) {
             };
         }
    
         @Override
         public void extendMessageConverters(List<HttpMessageConverter<?>> converters) 
         {
              converters.add(messagePackMessageConverter());
         }
     } 
    
seeker27
  • 117
  • 1
  • 3
  • 13