Announcement Announcement Module
Collapse
No announcement yet.
messageConverters not getting set on AnnotationMethodHandlerAdapter Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • messageConverters not getting set on AnnotationMethodHandlerAdapter

    I'm trying to use the @RequestBody annotation to convert post body xml to a Java object with Jibx. I've configured an AnnotationMethodHandlerAdapter bean with a list of converters, but it looks like the setMessageConverters() method on the AnnotationMethodHandlerAdapter object is never called (or at least it isnt stopping at my breakpoint in a debugger).

    So then when my request is sent, only the default handlers that were set in the constructor are being used for lookup and my request is failing with a 415 Unsupported Media Type exception.

    Please can someone tell me what is the correct way to make this work? Is it possible to use Jibx with the @RequestBody annotation?

  • #2
    Can you post your XML config for this one so we can see what you're declaring?

    This is what I have for converting JSON to Java object:

    Code:
    <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
    		<property name="order" value="0" />
    	</bean>
    	
    	<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    		<property name="webBindingInitializer">
    			<bean class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
    				<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.FormHttpMessageConverter" />
    				<bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter" />
    				<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />
    			</list>
    		</property>
    	</bean>
    	
    	<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />
    	<bean id="conversion-service" class="org.springframework.format.support.FormattingConversionServiceFactoryBean" />
    That's equivalent to:
    Code:
    <mvc:annotation-driven/>
    Can you also post a sample code from the affected Controller method?

    Comment


    • #3
      Another example:

      Code:
      <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
         <property name="messageConverters">
             <list>
                  <ref bean="marshallingConverter" />
             </list>
         </property>
      </bean>
      
      <bean id="marshallingConverter" class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
          <constructor-arg ref="jaxbMarshaller" />
          <property name="supportedMediaTypes" value="application/xml"/>
      </bean>
      
      <bean id="jaxbMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
          <property name="classesToBeBound">
      	  <list>
      	    <value>org.krams.tutorial.Person</value>
      	  </list>
          </property>
      </bean>

      Comment


      • #4
        messageConverters not getting set on AnnnotationMethodHandlerAdapter

        Here is my xml...it is not that much different.

        <!-- Configures support for @Controllers -->
        <mvc:annotation-driven />

        <context:component-scan base-package="com.xyz.c2c.web" />

        <bean class="org.springframework.web.servlet.mvc.annotat ion.DefaultAnnotationHandlerMapping">
        <property name="order" value="0" />
        <property name="useDefaultSuffixPattern" value="false" />
        </bean>

        <oxm:jibx-marshaller id="jibx-unmarshaller"
        target-class="com.xyz.c2c.xml.jibx.MyRequest"/>
        <oxm:jibx-marshaller id="jibx-marshaller"
        target-class="com.xyz.c2c.xml.jibx.MyResponse"/>

        <bean id="marshallingConverter" class="org.springframework.http.converter.xml.Mars hallingHttpMessageConverter">
        <!-- constructor-arg ref="jibx-marshaller"/>
        <constructor-arg ref="jibx-unmarshaller"/ -->

        <property name="marshaller" ref="jibx-marshaller"/>
        <property name="unmarshaller" ref="jibx-unmarshaller"/>
        </bean>

        <bean id="handlerAdapter"
        class="org.springframework.web.servlet.mvc.annotat ion.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
        <list>
        <ref bean="marshallingConverter"/>
        </list>
        </property>
        </bean>


        Here is the code is my Controller...it never gets in here. And I have tried this both with the headers value set as shown below and with it removed.

        @RequestMapping(value = "/create/{hostId}",
        method =RequestMethod.POST,
        headers="Accept=application/xml,plain/text")
        public @ResponseBody
        MyResponse myCreate(HttpServletRequest req,
        HttpServletResponse response,
        @PathVariable String hostId,
        @RequestBody MyRequest postBody)
        {
        }


        From the debugging that I have done it looks like the method is found and the RequestBody annotation is found, but the lookup for the annotation handler to use searches the original default list of annnotation handlers and does not find the "marshallingConverter" that I had intended to use. When I set a breakpoint in the setMessageConverters() method, the bean initialization code (at startup) never gets there. The method does not take a List...rather a HttpMessageConverter<?>[] array.

        Am I doing something wrong?

        Help is very much appreciated.

        Comment


        • #5
          I'm also stuck on this...

          Comment


          • #6
            Is your client invoking this via RestTemplate? if not whatever client code you are using the header information from your client needs to include Accept=application/xml

            There is Rest plugin for firefox, you can try from there and also use firebug to see what request you are actually sending.

            Comment


            • #7
              messageConverters not getting set on AnnnotationMethodHandlerAdapter

              Yes, the client is setting the content-type to "application/xml". In fact, this is confirmed when I set a breakpoint in the debugger in the method readWithMessageConverters(), at line 622

              MediaType contentType = inputMessage.getHeaders().getContentType()

              Results in the MediaType getting set equal to "application/xml".

              The problem appears to be in the loop to look up the messageConverter. The messageConverter that I configured does not appear to be listed and is certainly not found. The result is that an exception is thrown at line 647.

              throw new HttpMediaTypeNotSupportedException(contentType, allSupportedMediaTypes);

              Does anyone know if the @RequestBody annotation works with JiBX? Would anyone be able to provide a working example?

              Comment


              • #8
                I have somewhat similar problem which i posted with header message converters issue with json and xml both configured.
                When i was debugging at the same code it listed the xmlawareXXXX class but the list of supported media type doesnot include application/xml;charset=utf-8
                so ends up exactly with same error like you have.

                Comment


                • #9
                  I am also having the same problem: the jibxMarshaller is not being used. It could be a bug or we don't know how to use it properly.

                  I experimented with Jersey and it worked like a charm. If somebody asks me whether to use Spring 3 or Jersey to implement REST, I would advise them to use Jersey.

                  Comment


                  • #10
                    I have raised a jira issue https://jira.springsource.org/browse/SPR-7895

                    Comment


                    • #11
                      The mvc:annotation-driven element already defines an AnnotationMethodHandlerAdapter bean among others. Hence you're currently providing a second instance of it and that's not really what you want. If you need to customize things like message converters you'll need to use configuration like what scram posted above instead of the of mvc namespace.

                      There are tickets in JIRA already for improving the mvc namespace to allow registration of additional converters (see SPR-7091, SPR-7504, SPR-7191).

                      Comment


                      • #12
                        I to have the same issue with JiBX as noted above. All attempts at using JibxMarshaller return a 415 Unsupported Media Type. I have used two clients - one custom client based on 3.0.5 code base that previously worked against 3.0.5 server-side REST (and JaxB), and a REST browser plug-in. With JiBX, both fail in the same fashion with 415 Unsupported Media Type.

                        Will monitor this thread for updates...
                        Last edited by tgminc; Mar 3rd, 2014, 04:56 PM.

                        Comment

                        Working...
                        X