Results 1 to 2 of 2

Thread: ow to deal with jSON object wrapper in POST

  1. #1
    Join Date
    Aug 2010
    Posts
    1

    Default 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. #2

    Default

    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!
    Spring 3.X, Web Flow 2.X, Java 6, STS 2.7.0, Maven, OS X

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
  •