replacing AnnotationMethodHandlerAdapter with RequestMappingHandlerAdapter issue

后端 未结 3 1537
既然无缘
既然无缘 2021-02-03 10:46

I recently upgraded to spring 3.2 and noticed that AnnotationMethodHandlerAdapter had been deprecated in favor of RequestMappingHandlerAdapter. So I r

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-03 11:18

    So as it turns out, simple switching the bean definition doesn't work due to the fact that the RequestMappingHandlerAdapter is depending on a whole host of entities being created and configured. Spring, by default, is using a WebMvcConfigurationSupport entity to do all this default configuration, but simply creating my own bean version doesn't help because spring creates its own.

    My approach ended up being something along the lines of below, where I left basically all of the configuration up to spring's default, but then added my own converter. The only drawback is that it's switching xml configuration to javaconfig, but in my case, it's ok. There's an article here that describes something similar.

    @Configuration
    public class WebConfig extends WebMvcConfigurationSupport {
    
      @Bean
      public RequestMappingHandlerAdapter requestMappingHandlerAdapter() {
        RequestMappingHandlerAdapter handlerAdapter = super.requestMappingHandlerAdapter();
        handlerAdapter.getMessageConverters().add(0, getProtobufJsonMessageConverter());
        return handlerAdapter;
      }
    

提交回复
热议问题