Results 1 to 2 of 2

Thread: [Rest] How to unmarshall objects in controller?

  1. #1
    Join Date
    May 2009
    Posts
    5

    Question [Rest] How to unmarshall objects in controller?

    Hello everyone,

    In the ItemsController (with @Controller annotation) I have a method with the @RequestMapping(value="/items", method=RequestMethod.GET) annotation. It adds an attribute to a Model and returns a view name. This view is a org.springframework.web.servlet.view.xml.Marshalli ngView with the marshaller property set to a org.springframework.oxm.jaxb.Jaxb2Marshaller. This all works fine.

    Now for the RequestMethod.POST part. In the same controller I have a method public String post(RestItem restItem) with the @RequestMapping(value="/items", method=RequestMethod.POST) annotation.

    The restItem is sent by the client but not unmarshalled before calling the controller. It's properties are all null. This makes sense, I didn't configure an unmarshaller equivalent of org.springframework.oxm.jaxb.Jaxb2Marshaller anywhere.

    Where do I configure this?? What property to set? Or do I need to unmarshall myself?

    With kind regards,

    Jurrie

  2. #2
    Join Date
    May 2009
    Posts
    5

    Smile

    Quote Originally Posted by leadpumper View Post

    The restItem is sent by the client but not unmarshalled before calling the controller. It's properties are all null. This makes sense, I didn't configure an unmarshaller equivalent of org.springframework.oxm.jaxb.Jaxb2Marshaller anywhere.

    Where do I configure this?? What property to set? Or do I need to unmarshall myself?
    Don't you just hate it when you get your answer just after posting to a forum?

    The answer is to read the documentation: Spring Reference 15.3.2.5 Mapping the request body with the @RequestBody annotation

    In short: configure a org.springframework.web.servlet.mvc.annotation.Ann otationMethodHandlerAdapter and set its messageConverters property to contain a org.springframework.http.converter.xml.Marshalling HttpMessageConverter with your marshaller and unmarshaller. Then your @RequestBody annotation magically works

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
  •