Announcement Announcement Module
Collapse
No announcement yet.
REST, POST & XML Marshaller Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • REST, POST & XML Marshaller

    Hi,
    I'm trying new features of Spring 3. I'm creating a simple application with MVC. The application is composed by

    1. A controller with new REST feature (MarcoController)

    ================
    package it.mplebani.spring.rest.controller;

    import it.mplebani.spring.rest.data.Person;

    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestBod y;
    import org.springframework.web.bind.annotation.RequestMap ping;
    import org.springframework.web.bind.annotation.RequestMet hod;

    @Controller
    public class MarcoController {

    @RequestMapping(value="/people/get" ,method = RequestMethod.GET)
    public void all(){
    System.out.println("ALL");
    }


    @RequestMapping(value="/people/new",method = RequestMethod.POST)
    public void newPerson(@RequestBody Person person){
    System.out.println("I am here");
    }
    }

    ================

    2. A simple Domain Object (Person)

    ================
    package it.mplebani.spring.rest.data;

    import javax.xml.bind.annotation.XmlRootElement;

    @XmlRootElement
    public class Person {

    public String name;
    public String surname;
    }

    ================

    3. The servlet associated spring context

    ================

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

    <bean id="marshallingHttpMessageConverter"
    class="org.springframework.http.converter.xml.Mars hallingHttpMessageConverter"
    p:marshaller-ref="jaxb2Marshaller" p:unmarshaller-ref="jaxb2Marshaller" />

    <bean id="jaxb2Marshaller" class="it.mplebani.spring.rest.marshaller.MyJaxb2M arshaller">
    <property name="classesToBeBound">
    <list>
    <value>it.mplebani.spring.rest.data.Person</value>
    </list>
    </property>
    </bean>

    <context:component-scan base-package="it.mplebani.spring.rest" />

    ================

    4. the web.xml that associates the servletDispatcher with the spring context.

    Now, I'm testing the servlet endpoint with RESTClient. Calling the GET endpoint all works fine. Calling the POST endpoint I retrieve a "HTTP/1.1 415 Unsupported Media Type".

    Modifying the application changing the method newPerson the this new one

    @RequestMapping(value="/people/new",method = RequestMethod.POST)
    public void newPerson(String person){
    System.out.println("I am here");
    }

    All works fine. In debugging mode I can see the application stack passing through this method.

    I think this is a problem with @RequestBody or with the Marshaller.

    Any idea?

    Thanks
    -- Marco

  • #2
    Sending a POST request to a service endpoint that uses @RequestBody requires a bit more setup on the client side than is required for sending a GET request. This is due to the MessageConverter and OXM mechanisms that are used to implement @RequestBody.

    I think you need to set the "Accept" and "Content-Type" HTTP headers on the client side before sending the request. How you do this is specific to the test client you are using. I'm not familiar with RESTClient so I can't help there.

    Using Spring's web unit testing support classes, you would set up the request like this:

    Code:
      MockHttpServletRequest request = new MockHttpServletRequest();
      request.addHeader("Accept", "application/xml");
      request.addHeader("Content-Type", "application/xml");
      request.setMethod("POST");
      request.setRequestURI("/people/new");
      request.setContent("<person>...</person>");
    The addHeader() calls are the important part here.

    Scott

    Comment


    • #3
      Originally posted by scottyfred View Post
      I think you need to set the "Accept" and "Content-Type" HTTP headers on the client side before sending the request.
      I had a similar problem with the XML-marshalling when using RequestMethod.POST. Your hint about setting Content-Type and Accept explicitly to "application/xml" solved the problem. Thank you very much! M.

      Comment


      • #4
        Thanks for the help

        Is it required to define the @XmlType annotation on the bean object? I found that that was the reason my jaxb2Marshaller.supports(<Entity>.class) was returning false and I was getting a 415 error. I had the @XmlRootElement annotation on my entity already.

        Comment

        Working...
        X