Reputation: 6349
i am trying to get json data and store data in db and send json object about status of operation i am able to store data in db but my return json object is not working fine i am not getting json object my java code:
@RequestMapping(value = "/a",headers="Content-Type=application/json",method = RequestMethod.POST)
@ResponseBody
public JSONObject data(@RequestBody String load)
{
org.json.JSONObject obj = new org.json.JSONObject();
obj.put("Status", "Success");
obj.put("Details","DB updated");
return obj;
}
Upvotes: 0
Views: 2346
Reputation: 1641
In your @RequestMapping
annotation define produces = MediaTyp.APPLICATION_JSON_VALUE
. Your method should then just return a simple Map
.
What is returned is actually controlled by the accepts Header in the request. So as an alternativ you could always ensure that your request asks for the right typ. But setting produces in the annotation is in my opinion a good idea as Spring does some auto conversions based on libraries available on the classpath. This might cause security issues if you do not control the type by hand.
edit:
Instead of a simple Map
you could also just return any Java Object as long as it can be serialized by Jackson. You can control serialization using annotation in the Object class in this case.
Also you need the Jaclson library on the classpath for this to work (should be the case if you use a basic Spring Boot Web App).
Here is the offical Spring guide on how to build sutch a service: http://spring.io/guides/gs/rest-service/
Upvotes: 1