Better way to use Json object from @requestBody in controller instead of mapping to POJO

We usually map an object from @RequestBody to some POJO on the controller. To render to POJO, we need to know the fields from the @RequestBody object. So my question is what might be the best way to use this object inside the controller IF YOU DON'T KNOW THE FIELDS INSIDE TO REQUEST.

What is it like?

@RequestMapping(value = "/students", method = RequestMethod.POST, consumes = "application/json")
public @ResponseBody Student getStudent(@RequestBody String json) {
      // then parse the json string into object...
} 

      

Share some innovative ideas. Thanks in advance.

+3


source to share


2 answers


Updated answer

Based on the updated question description and if you are already using Jackson then the following code will give you a json string map.



ObjectMapper mapper = new ObjectMapper();
Map<String,Object> map = mapper.readValue(json, Map.class);

      

Note. ... I created a new instance ObjectMapper

, you can use the ObjectMapper bean if you have already defined it for your application.

+1


source


You can create JSONObject. Follow http://www.json.org/javadoc/org/json/JSONObject.html



0


source







All Articles