Uploaded image for project: 'Spring Framework'
  1. Spring Framework
  2. SPR-7091

AnnotationDrivenBeanDefinitionParser should allow injection of custom HttpMessageConverter's

    Details

    • Type: Improvement
    • Status: Resolved
    • Priority: Minor
    • Resolution: Duplicate
    • Affects Version/s: 3.0.1, 3.0.2
    • Fix Version/s: None
    • Component/s: Web
    • Labels:
      None
    • Last commented by a User:
      false

      Description

      1. There should be a way to inject custom http message converters into AnnotationDrivenBeanDefinitionParser. Currently its hardcoded and set to a private method

      private ManagedList<RootBeanDefinition> getMessageConverters(Object source)

      { ... }

      2. AnnotationMethodHandlerAdapter has hardcoded the HttpMessageConverters as well, when replacing the bean with something like

      <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
      <property name="messageConverters">
      <list>
      <ref bean="customConverter"/>
      </list>
      </property>
      </bean>

      The AnnotationMethodHandlerAdapter bean is never replaced within the Spring Context.

      Please let me know if there is a different solution?

        Issue Links

          Activity

          Hide
          felthy Peter Feltham added a comment -

          I've worked around this by referring to the schema-generated bean by its name "org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter#0" and using the MethodInvokingFactoryBean to call its setMessageConverters method - but this feels like a hack. As a user of the AnnotationMethodHandlerAdapter, I would expect my HttpMessageConverters to be auto-detected by type - in the same way as HandlerMapping and HandlerAdapter instances are auto-detected by the DispatcherServlet. I'd like to see this added in a future release.

          Show
          felthy Peter Feltham added a comment - I've worked around this by referring to the schema-generated bean by its name "org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter#0" and using the MethodInvokingFactoryBean to call its setMessageConverters method - but this feels like a hack. As a user of the AnnotationMethodHandlerAdapter, I would expect my HttpMessageConverters to be auto-detected by type - in the same way as HandlerMapping and HandlerAdapter instances are auto-detected by the DispatcherServlet. I'd like to see this added in a future release.
          Hide
          simonwg Simon Wong added a comment -

          The following works fine for me. Since <mvc:annotation-driven / > did not cater the MessageConverter for ExceptionResolver, I have set it explicitly

          <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"
          p:order="1" />

          <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
          <property name="webBindingInitializer">
          <bean class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
          <property name="conversionService" ref="conversionService" />
          <property name="validator" ref="validator" />
          </bean>
          </property>
          <property name="messageConverters">
          <list>
          <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter" />
          <bean class="org.springframework.http.converter.StringHttpMessageConverter" />
          <bean class="org.springframework.http.converter.ResourceHttpMessageConverter" />
          <bean class="org.springframework.http.converter.FormHttpMessageConverter" />
          <bean class="com.google.protobuf.spring.http.ProtobufHttpMessageConverter" />
          <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />
          </list>
          </property>
          </bean>

          <bean id="annotationMethodHandlerExceptionResolver" class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver"
          p:order="1">
          <property name="messageConverters">
          <list>
          <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter" />
          <bean class="org.springframework.http.converter.StringHttpMessageConverter" />
          <bean class="org.springframework.http.converter.ResourceHttpMessageConverter" />
          <bean class="org.springframework.http.converter.FormHttpMessageConverter" />
          <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />
          </list>
          </property>
          </bean>

          Show
          simonwg Simon Wong added a comment - The following works fine for me. Since <mvc:annotation-driven / > did not cater the MessageConverter for ExceptionResolver, I have set it explicitly <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" p:order="1" /> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="webBindingInitializer"> <bean class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer"> <property name="conversionService" ref="conversionService" /> <property name="validator" ref="validator" /> </bean> </property> <property name="messageConverters"> <list> <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter" /> <bean class="org.springframework.http.converter.StringHttpMessageConverter" /> <bean class="org.springframework.http.converter.ResourceHttpMessageConverter" /> <bean class="org.springframework.http.converter.FormHttpMessageConverter" /> <bean class="com.google.protobuf.spring.http.ProtobufHttpMessageConverter" /> <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" /> </list> </property> </bean> <bean id="annotationMethodHandlerExceptionResolver" class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver" p:order="1"> <property name="messageConverters"> <list> <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter" /> <bean class="org.springframework.http.converter.StringHttpMessageConverter" /> <bean class="org.springframework.http.converter.ResourceHttpMessageConverter" /> <bean class="org.springframework.http.converter.FormHttpMessageConverter" /> <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" /> </list> </property> </bean>
          Hide
          dma_k Dmitry Katsubo added a comment -

          I vote for this issue. It would be nice if <mvc:annotation-driven /> can detect already registered message converters. In my case I would like to customize MappingJacksonHttpMessageConverter but I can't do it easily, as this class is created new by AnnotationDrivenBeanDefinitionParser. I propose that (a) it should check the context for unknown implementations of HttpMessageConverter's and (b) it should use already defined already customized beans for "known" HttpMessageConverter's (e.g. FormHttpMessageConverter, ...). Maybe this problem should be solved by AnnotationMethodHandlerAdapter itself, which can collect all HttpMessageConverter's just after context has been instantiated (ApplicationContextAware). Relative to SPR-7190 and SPR-6750, as having autodetection implemented solves them.

          Show
          dma_k Dmitry Katsubo added a comment - I vote for this issue. It would be nice if <mvc:annotation-driven /> can detect already registered message converters. In my case I would like to customize MappingJacksonHttpMessageConverter but I can't do it easily, as this class is created new by AnnotationDrivenBeanDefinitionParser . I propose that (a) it should check the context for unknown implementations of HttpMessageConverter 's and (b) it should use already defined already customized beans for "known" HttpMessageConverter 's (e.g. FormHttpMessageConverter , ...). Maybe this problem should be solved by AnnotationMethodHandlerAdapter itself, which can collect all HttpMessageConverter 's just after context has been instantiated ( ApplicationContextAware ). Relative to SPR-7190 and SPR-6750 , as having autodetection implemented solves them.
          Hide
          rstoya05-aop Rossen Stoyanchev added a comment -

          This issue should be resolved with the completion of SPR-7504.

          Show
          rstoya05-aop Rossen Stoyanchev added a comment - This issue should be resolved with the completion of SPR-7504 .

            People

            • Assignee:
              rstoya05-aop Rossen Stoyanchev
              Reporter:
              krasaee Kasra Rasaee
              Last updater:
              Trevor Marshall
            • Votes:
              4 Vote for this issue
              Watchers:
              5 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:
                Days since last comment:
                6 years, 43 weeks, 6 days ago