Announcement Announcement Module
Collapse
No announcement yet.
[Rest] How to unmarshall objects in controller? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • [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
    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

    Comment

    Working...
    X