How to post JSON to Spring server?

I get a list of object from server. One key of this objects is called body, and it is a json. It is almost impossible for me to know its keys : the key name and size of it (body) are different.

Example of data

data from server = [object, object, object,.....]

object = {
  id: 1,
  name: "xyz",
  body": {
    x: "xyz",
    y: "xyz",
    z: "xys",
  }
}

I edit the body and then post it to the server together with the id of the object. Until here it is fine. I can send the request but can not handle the Requestparam which is of type json.

How to handle this post request on the backend built in Java and Spring?

1 answer

  • answered 2017-08-17 17:28 ced-b

    You have to add a @RequestBody annotation to the method on the Controller such as:

    @RequestMapping ("url/to/save")
    @ResponseBody
    public ResponseObject send (@RequestBody RequestObject myRequestObject)
    {
       //do something
    
       return new ResponseObject ();
    }
    

    The RequestObject would be the Java Class mapping to the JSON you want to save. The ResponseObject is whatever you want to return, you could also return void but Firefox has issues with that sometimes.