【问题标题】:Adding msgpack as content negotiation to spring mvc + boot将 msgpack 作为内容协商添加到 spring mvc + boot
【发布时间】:2016-03-22 21:06:08
【问题描述】:

我正在尝试添加 msgpack 二进制数据格式作为内容协商选项。 Json 和 Xml 开箱即用。我尝试将 jackson msgpack 映射器添加为 bean,就像在 this examle 中一样,但它不起作用。当我将Accept: application/x-msgpack 标头添加到我的请求时,406 Not Acceptable 代码被返回。

这是我的 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"));
    }

}

我没有为我的 DTO 对象添加任何特殊的注释,我的控制器也没有什么特别之处。

我的 msgpack 依赖是:

org.msgpack:jackson-dataformat-msgpack:0.7.0-p3

【问题讨论】:

    标签: java spring spring-boot jackson msgpack


    【解决方案1】:

    显然 bean 注入不起作用(如果有人向我展示如何自动注入新的 HttpMessageConverter,我会很高兴)。所以我手动添加了它:

    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);
        }
    
        //...
    

    【讨论】:

      【解决方案2】:

      可以通过两种方式完成 -

      1. 使用 spring boot 只需创建一个 HttpMessageConverter 类型的 @Bean 就可以完成这项工作。 spring boot 会自动将其添加到消息转换器列表中。 (但这样做也意味着如果现有应用程序在未提供 Accept 标头时返回“application/json”作为默认响应,那么它现在将返回默认“application/x-msgpack”)

         @Bean    
         public HttpMessageConverter messagePackMessageConverter() {
                 MediaType msgPackMediaType = new MediaType("application", "x-msgpack");
                 return new AbstractJackson2HttpMessageConverter(new ObjectMapper(new 
                      MessagePackFactory()), msgPackMediaType) {
                 };
         }
        
      2. 此解决方案类似于上述使用 WebMvcConfigurerAdapter 的解决方案,但我建议不要使用 configureMessageConverters,而是使用 extendMessageConverters,因为使用 configureMessageConverters 会关闭 spring 完成的默认转换器注册。用法示例 -

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-03-01
        • 1970-01-01
        • 2016-01-05
        • 2014-06-15
        • 2011-04-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多