Reputation: 465
I have a Spring API that returns JSON, the controller methods builds the JSON string using StringBuilder
and returns the JSON string which can be viewed as raw JSON in the browser.
Is there a better way of creating/returning the results in JSON without using JacksonJsonView
?. If I just put the results in HashMap<String, String>
and return the map does that work? I tried it but did not help. Is there any good way, any one can suggest please?
Upvotes: 2
Views: 752
Reputation: 13181
Spring 3.0 introduced the @ResponseBody
annotation which converts arbitrary data structures to JSON behind the scenes, without your involvement. Just make sure Jackson is on the classpath and you're good to go. For example:
@RequestMapping(value="/getJson", method=RequestMethod.GET)
@ResponseBody
public Map<String, Object> getJson(@RequestParam String something) {
Map<String, Object> output = new HashMap<String, Object>();
output.put("date", new Date());
output.put("input", something);
return output;
}
More info in spring.io blog article.
Upvotes: 4