Home » Java » How to access internal json object in Spring Rest Request

How to access internal json object in Spring Rest Request

Posted by: admin October 22, 2018 Leave a comment

Questions:

I want to create one API which format will be like below.

{
           "jsonObject": {
                     //some json object     
           },
           "key": "SampleKey",
           "jsonDataKey": "SampleDataKey"
}

for this I have created the RequestBody class as below.

public class ParentJsonInfo {
public String getKey() {
    return key;
}

public void setKey(String key) {
    this.key = key;
}

private String key;

public JsonObject getJsonData() {
    return jsonData;
}

public void setJsonData(JsonObject jsonData) {
    this.jsonData = jsonData;
}

private JsonObject jsonData;

public String getJsonDataKey() {
    return jsonDataKey;
}

public void setJsonDataKey(String jsonDataKey) {
    this.jsonDataKey = jsonDataKey;
}

private String jsonDataKey;

}

but unfortunately I am not getting any data inside the json object of my class. M I doing anything wrong. please guide me to how should i access the data inside that object.

Here is the controller method code.

@RequestMapping(value = "/postNews", method = RequestMethod.POST)
public Greeting greeting(@RequestBody ParentJsonInfo parentJsonInfo) {
 Jsonobject jsonObject= parentJsonInfo.getjsonObject();
}
Answers: