Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: messageConverters not getting set on AnnotationMethodHandlerAdapter

  1. #1
    Join Date
    May 2007
    Posts
    9

    Question 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. #2
    Join Date
    Dec 2010
    Posts
    315

    Default

    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?

  3. #3
    Join Date
    Dec 2010
    Posts
    315

    Default

    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>

  4. #4
    Join Date
    May 2007
    Posts
    9

    Default 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.

  5. #5
    Join Date
    Sep 2007
    Posts
    12

    Default

    I'm also stuck on this...

  6. #6
    Join Date
    Oct 2009
    Location
    Minneapolis, MN
    Posts
    137

    Default

    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.

  7. #7
    Join Date
    May 2007
    Posts
    9

    Default 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?

  8. #8
    Join Date
    Oct 2009
    Location
    Minneapolis, MN
    Posts
    137

    Default

    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.

  9. #9

    Default

    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.

  10. #10

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •