Skip to content

AnnotationDrivenBeanDefinitionParser should allow injection of custom HttpMessageConverter's [SPR-7091] #11751

Closed

Description

@spring-projects-issues

Kasra Rasaee opened SPR-7091 and commented

  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) { ... }

  1. 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?


Affects: 3.0.1, 3.0.2

Issue Links:

4 votes, 5 watchers

Activity

  1. spring-projects-issues commented on Jul 20, 2010

    @spring-projects-issues
    CollaboratorAuthor

    Peter Feltham commented

    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.

  2. spring-projects-issues commented on Jul 20, 2010

    @spring-projects-issues
    CollaboratorAuthor

    Simon Wong commented

    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>
    
  3. spring-projects-issues commented on Jul 22, 2010

    @spring-projects-issues
    CollaboratorAuthor

    Dmitry Katsubo commented

    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 #11850 and #11416, as having autodetection implemented solves them.

  4. spring-projects-issues commented on Jan 25, 2011

    @spring-projects-issues
    CollaboratorAuthor

    Rossen Stoyanchev commented

    This issue should be resolved with the completion of #12161.

  5. added
    type: enhancement
    status: duplicate
    in: web
    on Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

in: webstatus: duplicatetype: enhancement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions