Spring Framework
  1. Spring Framework
  2. SPR-7895

Custom message converters registered with AnnotationMethodHandlerAdapter are not used, only the default ones are used.

    Details

    • Type: Bug Bug
    • Status: Resolved
    • Priority: Major Major
    • Resolution: Invalid
    • Affects Version/s: 3.0.5
    • Fix Version/s: None
    • Component/s: OXM
    • Labels:
      None

      Description

      Custom message converters registered with AnnotationMethodHandlerAdapter are not used, only the default ones registered with <mvc:annotation-driven /> are used. I found that the setMessageConverters() method of AnnotationMethodHandlerAdapter class is called two times, one for the default and another one for the bean defined in the spring xml file. The one defined in the spring xml file is never used when doing actual marshalling and unmarshalling.

      For example the following jibx message converter is not used:

      <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
      <property name="messageConverters">
      <util:list id="beanList">
      <ref bean="marshallingHttpMessageConverter" />
      </util:list>
      </property>

      </bean>

      <bean id="marshallingHttpMessageConverter" class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
      <constructor-arg ref="jixbUserMarshaller" />
      <property name="supportedMediaTypes" value="text/xml" />
      </bean>

      <oxm:jibx-marshaller id="jixbUserMarshaller" target-class="com.company.restpoc.model.User" />

      In my opinion the jibx support in Spring OXM is rather poor. We need to define a separate converter for each target-class. We don't need to do that kind of thing when using Jersey with Jibx. We just need two @Provider classes that implement MessageBodyReader and MessageBodyWriter. I have attached JIBXBodyReader and JIBXBodyWriter for your reference.

      1. JIBXBodyReader.java
        1 kB
        Jack Woods
      2. JIBXBodyWriter.java
        2 kB
        Jack Woods

        Activity

        Hide
        Rossen Stoyanchev added a comment -

        The custom message converters are ignored because the AnnotationMethodHandlerAdapter explicitly defined in the configuration is in addition to the one created by the <mvc:annotation-driven/> element.

        Show
        Rossen Stoyanchev added a comment - The custom message converters are ignored because the AnnotationMethodHandlerAdapter explicitly defined in the configuration is in addition to the one created by the <mvc:annotation-driven/> element.
        Hide
        Jack Woods added a comment -

        That means we can only use the default message converts created by <mvc:annotation-driven/> with @ResponseBody ? If that is true then this issue can be classified as an enhancement instead of a bug. Either add the jaxb message converter as a default converter of make it possible for addtional message converters to be added to the default convert list. Also it would be good to mention this somewhere in the documentation (apologies if it is already mentioned).

        Show
        Jack Woods added a comment - That means we can only use the default message converts created by <mvc:annotation-driven/> with @ResponseBody ? If that is true then this issue can be classified as an enhancement instead of a bug. Either add the jaxb message converter as a default converter of make it possible for addtional message converters to be added to the default convert list. Also it would be good to mention this somewhere in the documentation (apologies if it is already mentioned).
        Hide
        Jack Woods added a comment -

        I saw your comment in the forum. As you said this issue has already been raised by other people (SPR-7091, SPR-7504, SPR-7191). Feel free to delete this issue.

        Show
        Jack Woods added a comment - I saw your comment in the forum. As you said this issue has already been raised by other people ( SPR-7091 , SPR-7504 , SPR-7191 ). Feel free to delete this issue.
        Hide
        Mcihal Wroblewski added a comment -

        I had problem with change alwaysUseFullPath for AnnotationMethodHandlerAdapter. Is was loaded only with four default messageConverters.

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

        <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" p:alwaysUseFullPath="true" p:order="3">
        <property name="messageConverters">
        <list>
        <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/>
        <ref bean="stringHpptConverter"/>
        <bean class="org.springframework.http.converter.ResourceHttpMessageConverter"/>
        <bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter"/>
        <bean class="org.springframework.http.converter.xml.XmlAwareFormHttpMessageConverter"/>
        <bean class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter"/>
        <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>
        <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/>
        </list>
        </property>
        <!--
        org.springframework.http.converter.ByteArrayHttpMessageConverter@150ff8e3
        org.springframework.http.converter.StringHttpMessageConverter@32f3fb36
        org.springframework.http.converter.ResourceHttpMessageConverter@2471809b
        org.springframework.http.converter.xml.SourceHttpMessageConverter@3c5a5e89
        org.springframework.http.converter.xml.XmlAwareFormHttpMessageConverter@68f457aa
        org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter@1fcd3ed6
        org.springframework.http.converter.json.MappingJacksonHttpMessageConverter@5b87851a
        -->
        </bean>

        <bean id="stringHpptConverter" class="org.springframework.http.converter.StringHttpMessageConverter">
        <property name="writeAcceptCharset" value="false"/>
        </bean>

        It resolved my problem.

        Show
        Mcihal Wroblewski added a comment - I had problem with change alwaysUseFullPath for AnnotationMethodHandlerAdapter. Is was loaded only with four default messageConverters. <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" p:order="3" p:alwaysUseFullPath="true"/> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" p:alwaysUseFullPath="true" p:order="3"> <property name="messageConverters"> <list> <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/> <ref bean="stringHpptConverter"/> <bean class="org.springframework.http.converter.ResourceHttpMessageConverter"/> <bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter"/> <bean class="org.springframework.http.converter.xml.XmlAwareFormHttpMessageConverter"/> <bean class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter"/> <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/> <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/> </list> </property> <!-- org.springframework.http.converter.ByteArrayHttpMessageConverter@150ff8e3 org.springframework.http.converter.StringHttpMessageConverter@32f3fb36 org.springframework.http.converter.ResourceHttpMessageConverter@2471809b org.springframework.http.converter.xml.SourceHttpMessageConverter@3c5a5e89 org.springframework.http.converter.xml.XmlAwareFormHttpMessageConverter@68f457aa org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter@1fcd3ed6 org.springframework.http.converter.json.MappingJacksonHttpMessageConverter@5b87851a --> </bean> <bean id="stringHpptConverter" class="org.springframework.http.converter.StringHttpMessageConverter"> <property name="writeAcceptCharset" value="false"/> </bean> It resolved my problem.
        Hide
        Rossen Stoyanchev added a comment -

        Closing as invalid since defining both an mvc:annotation-driven element an AnnotationMethodHandlerAdapter results in duplicate configuration rather than a customization of the message converters. Note however that the ability to register custom message converters is available in Spring 3.1 M1 (see SPR-7504).

        Show
        Rossen Stoyanchev added a comment - Closing as invalid since defining both an mvc:annotation-driven element an AnnotationMethodHandlerAdapter results in duplicate configuration rather than a customization of the message converters. Note however that the ability to register custom message converters is available in Spring 3.1 M1 (see SPR-7504 ).

          People

          • Assignee:
            Rossen Stoyanchev
            Reporter:
            Jack Woods
            Last updater:
            Trevor Marshall
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:
              Days since last comment:
              3 years, 9 weeks, 2 days ago