Announcement Announcement Module
Collapse
No announcement yet.
What is the correct setup for REST with XML Payload? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • What is the correct setup for REST with XML Payload?

    Hi,

    I started using Spring REST support that came with Spring release RC1 and I haven't worked with Spring MVC before. So please correct me if I am doing something wrong here.

    I want to map/convert the XML payload to the parameter of my method. So lets say I want to make a POST request to the url "/locations/cities" with the payload :-
    Code:
    <city>
      <name>Toronto</name>
      <country>Canada</country>
    </city>
    I want the City Object(which is annotated by @XmlRootElement for jaxb marshalling) set to my method :-
    Code:
    @RequestMapping("/locations/cities")
    @ResponseBody
    public City createCity(@RequestBody City city) {
        return repo.save(city);
    }
    I also want the returned object to be marshalled out to the response with the jaxb marshaller. In order to achieve this I have made the following configuration :-

    Code:
     <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
            <property name="messageConverters">
                <util:list id="beanList">
                    <bean class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
                        <constructor-arg ref="marshaller"/>
                    </bean>
                    <bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
                </util:list>
            </property>
    </bean>
    
    <oxm:jaxb2-marshaller id="marshaller">
            <oxm:class-to-be-bound name="location.model.City"/>
    </oxm:jaxb2-marshaller>
    I am able to get what I want with just the above configuration (and no ViewResolver) which brings me to my question. Is this the best way to deal with REST+XML?. When do we need a ViewResolver and why (in the context of REST with XML Payload)?

    I also want to set/assume the default Accepted MediaType as text/xml instead of forcing the client to specify that each time he makes a REST call. How would I achieve this?

  • #2
    Yours looks like a viable approach. I'm actually surprised it works without a view resolver. I wonder if the <oxm:jaxb2-marshaller /> provides an implicit MarshallingView and corresponding ViewResolver.

    If you want to support multiple view technologies (such as using a JSP file for rendering HTML views and using JAXB to render XML views), then defining explicit view resolvers will become important.

    Here's an example I wrote in which the HTTP Accept header may specify "text/html" or "application/xml", which controls which of two view resolvers is used to render the view: http://earldouglas.com/node/27

    Comment


    • #3
      I'm actually surprised it works without a view resolver. I wonder if the <oxm:jaxb2-marshaller /> provides an implicit MarshallingView and corresponding ViewResolver.
      Using the @RequestBody and @ResponseBody annotations on the Controller requires the use of an MessageConverter (usually MarshallingHttpMessageConverter). In this configuration, the MessageConverter converts the response object from the controller to the XML without using a View or ViewResolver.

      If you do not use @RequestBody and @ResponseBody (i.e. use @RequestParam or don't use any annotation and let MVC figure it out) then a ViewResolver and View must be configured to get the response marshalled correctly and a MessageConvert is not required (or used even if it is configured).

      Comment

      Working...
      X