replacing AnnotationMethodHandlerAdapter with RequestMappingHandlerAdapter issue

后端 未结 3 1536
既然无缘
既然无缘 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;
      }
    
    0 讨论(0)
  • 2021-02-03 11:24

    Use "<mvc:annotation-driven/>" in the spring configuration file instead of writing your own implementation of WebMvcConfigurationSupport

    example

        <mvc:annotation-driven/>
    <context:component-scan base-package="com.springapp.mvc"/>
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/pages/"/>
            <property name="suffix" value=".jsp"/>
        </bean>
    
        <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
            <property name="messageConverters">
                <list>
                    <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
                    <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/>
                    <bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter"/>
                    <bean class="org.springframework.http.converter.FormHttpMessageConverter"/>
                    <bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
                </list>
            </property>
    
        </bean>
    
    0 讨论(0)
  • 2021-02-03 11:28
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
    import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
    @Configuration
    public class WebConfig extends WebMvcConfigurationSupport {
      @Override
      protected RequestMappingHandlerAdapter createRequestMappingHandlerAdapter() {
        return new XXXXRequestMappingHandlerAdapter();
      }
    }
    
    0 讨论(0)
提交回复
热议问题