Another simple way to return JSON object is by using jackson-mapper-asl. Similar to how we can map server-bound post, this method can also be used to write response.
Firstly, on your Spring MVC enabled project, add following maven dependency:
org.codehaus.jackson jackson-mapper-asl 1.9.12
Spring can automatically convert your POJO into a json string. So say we have this data object we want to return:
public class Customer {
private String name = "";
private String email = "";
// getters & setters...
}
And this is the controller request mapping method. Important bits here is the method returns a POJO object directly, and it is annotated with annotation.
("/customer/{id}")
public Customer getCustomer(("id") long id) {
Customer customer = // Search customer by given id through repository..
return customer;
}
On the client side the returned JSON will be something like this:
{
name = "Tom",
email = ""
}
Thanks garry for this post, I have posted similar post with ExtJS as client. Pls check
http://www.techzoo.org/spring-framework/spring-mvc-requestbody-json-example.html