Reputation: 45
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();
}
Upvotes: 0
Views: 1167
Reputation: 1
u can modify like this
public Greeting greeting(@RequestBody String parentJsonInfo) {
// parse string to jsonobject
}
Upvotes: 0
Reputation: 8626
The problem you are having is that you are trying to deserialize jsonObject
which is from your json, but your field is called jsonData
.
As @Mushtu mentioned, you need to rename that field.
Here is your ParentJsonInfo with a few adjustments:
jsonData
to jsonObject
ParentJsonInfo:
public class ParentJsonInfo {
private String key;
private JsonObject jsonObject;
private String jsonDataKey;
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public JsonObject getJsonObject() {
return jsonObject;
}
public void setJsonObject(JsonObject jsonObject) {
this.jsonObject = jsonObject;
}
public String getJsonDataKey() {
return jsonDataKey;
}
public void setJsonDataKey(String jsonDataKey) {
this.jsonDataKey = jsonDataKey;
}
}
JsonObject:
public class JsonObject {
private Map<String, Object> other = new HashMap<>();
@JsonAnyGetter
public Map<String, Object> getProperties() {
return other;
}
@JsonAnySetter
public void set(String name, String value) {
other.put(name, value);
}
}
Upvotes: 2