mhndev :
Is there a way to get request body(JSON) parameters without using a POJO object for each request? I have two types of requests, in many of these request what I want is get a parameter from request, for example something like this:
{"name": "Mike", "Age":25}
request.getBodyParameter("name");
and for some of my requests I want to convert the input json to a JAVA hash map.
Hades :
@RequestMapping(value = "/foo", method = RequestMethod.POST, consumes = "application/json")
public Status getJsonData(@RequestBody JsonObject jsonData){
}
from jsonData
yo can do jsonData.getString("name")
or you can convert this into map
HashMap<String,Object> result =
new ObjectMapper().readValue(jsonData, HashMap.class);
Update
public Status getJsonData(@RequestBody JsonNode jsonNode){
String name = jsonNode.get("name").asText();
}
For conversion to map
ObjectMapper mapper = new ObjectMapper();
Map<String, Object> result = mapper.convertValue(jsonNode, new TypeReference<Map<String, Object>>(){});
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=371670&siteId=1