Getting JSON from Spring 3 Controller

Spring 3 controller can generate JSON response transparently. All you need to do is to have jackson jar files in the web app classpath.  For example, a controller will look like:

@RequestMapping(value=”/example/get.action”, method=RequestMethod.GET)
public @ResponseBody Pizza getPizza() {
Pizza pizza = … // create object here
return pizza;

To get pizza object in JSON, the request header should have attribute Accept with value application/json.  Then json text will be returned by the controller in the response content, for example:


“base”:{“name”:”thin crust”},