mahan
mahan

Reputation: 15035

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?

Upvotes: 0

Views: 188

Answers (1)

ced-b
ced-b

Reputation: 4065

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.

Upvotes: 1

Related Questions