Results 1 to 3 of 3

Thread: How to perform JAXB2 marshalling of input request in REST with Spring MVC3?

  1. #1

    Default How to perform JAXB2 marshalling of input request in REST with Spring MVC3?

    (This question is posted on Stackoverflow too, http://stackoverflow.com/questions/1...th-spring-mvc3)

    I am new to Spring MVC (Spring 3) and REST, and I am trying out a little toy app to try out GET and POST web services. I have read the Spring's official reference, and found these questions in Stackoverflow, http://stackoverflow.com/questions/8...st-application , http://stackoverflow.com/questions/4...n-spring-mvc-3 but I am still stucked in making it works. Can anyone give me hints on what I missed?

    My Controller is like this:
    Code:
        @Controller
        @RequestMapping(value = "/echo")
        public class EchoControllerImpl implements EchoController {
            @RequestMapping(method = RequestMethod.POST, value = "/echoByPost")
            public ModelAndView echoByPost(@ModelAttribute EchoDto input) {
                // ... 
            }
        }
    I have putted corresponding converter in the app ctx:

    Code:
        <mvc:annotation-driven />
        <bean id="jaxb2Marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller" >
            <property name="classesToBeBound">
                <list>
                    <value>foo.EchoDto</value>
                </list>
            </property>
        </bean>
        <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
            <property name="messageConverters">
                <list>
                    <ref bean="marshallingHttpMessageConverter" />
                    <ref bean="stringHttpMessageConverter" />
                </list>
            </property>
        </bean>
    
        <bean id="stringHttpMessageConverter"
            class="org.springframework.http.converter.StringHttpMessageConverter" />
    
        <bean id="marshallingHttpMessageConverter"
            class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
            <property name="marshaller" ref="jaxb2Marshaller" />
            <property name="unmarshaller" ref="jaxb2Marshaller" />
        </bean>
        <!-- other beans like ViewResolvers  -->
    I even tried to add these to web.xml as I saw somewhere mentioning about it (though I don't really know what does it means)
    Code:
        <filter>
            <filter-name>httpPutFormFilter</filter-name>
            <filter-class>org.springframework.web.filter.HttpPutFormContentFilter</filter-class>
        </filter>
    
        <filter-mapping>
            <filter-name>httpPutFormFilter</filter-name>
            <servlet-name>spring-rest</servlet-name>
        </filter-mapping>

    Then I tried to invoke my web service by Curl:
    Code:
    `curl -d "echoDto=<EchoDto><message>adrian</message></EchoDto>" http://localhost:8001/foo/rest/echo/echoByPost.xml`
    I found that my incoming object is not created through unmarshalling of JAXB2. Instead, seems that the ctor EchoDto(String) is called which the contains whole request xml message.

    (I also tried annotating the parameter with @RequestBody instead but it is even worse, that I cannot even invoke the controller method)

    Can someone tell me what I have missed?

    The Jaxb2Marshaller is setup correctly with the DTO class, coz I am able to use it as returned model object in case of another GET REST webserivce call.

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    You have to use @RequestBody instead of @ModelAttribute if you have a problem with that post that problem here. @ModelAttribute is only usable for forms and form binding.

    Also your controller is mapped to /echo/echoByPost and not /echo/echoByPost.xml ...
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3

    Default

    Quote Originally Posted by Marten Deinum View Post
    You have to use @RequestBody instead of @ModelAttribute if you have a problem with that post that problem here. @ModelAttribute is only usable for forms and form binding.

    Also your controller is mapped to /echo/echoByPost and not /echo/echoByPost.xml ...
    Thanks Marten,

    I have got the result by combining what you said and the answer from StackOverflow (http://stackoverflow.com/a/11271644/395202):
    1) yes I should use @RequestBody
    2) I should provide header of "Content-Type: application/xml"
    3) I don't need "echoDto=" in the content.

    However, using /echo/echoByPost.xml seems alright coz it seems only affect the selection of view.

    Thanks again for your hints

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
  •