Announcement Announcement Module
Collapse
No announcement yet.
ow to deal with jSON object wrapper in POST Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • ow to deal with jSON object wrapper in POST

    Hello,

    I'm using Spring MVC 3.0.3 on backend and Ext JS on frontend.

    When creating a new record in grid, Ext send a POST request with following JSON string:

    {"data":{"name":"aa","label":"a","description":"a" }}

    The record is wrapped in a JSON object named "data".

    From what I have seen and read, Spring Jackson message converter expects the objet directly as below:

    Code:
    {"name":"aa","label":"a","description":"a"}
    I implemented a wrapper class ExtJsRequest in order to mimic the JSON wrapping.

    Code:
    public class ExtJsRequest {
    
    	private Application data;
    
    	public void setData(Application data) {
    		this.data = data;
    	}
    
    	public Application getData() {
    		return data;
    	}
    }
    And I used @RequestBody as below:

    Code:
    @RequestMapping(value = "/application", method = POST)
    public ModelAndView create(@RequestBody ExtJsRequest extRequest, HttpServletResponse response) {
    	...
    }
    An alternative would be to customize the Ext.data.JsonWriter to suppress object wrapping on client side.

    But I was wondering if there is a more convenient way to do it in Spring: in the controller or message converter?

    Thanks,

    Phil

  • #2
    After a debugging session I think, that your method should be like this:
    Code:
    @RequestMapping(value = "/application", method = RequestMethod.POST)
    public ModelAndView create(@ModelAttribute("data") EntityClass data, HttpServletResponse response) {
    	...
    }
    EntityClass should be the Class of your Entity, like Pet oder Account ...
    Another hint: The Attribute of ModelAttribute maybe not optional!

    Comment

    Working...
    X