Spring MVC and AJAX with JSON
Posted on : 15-02-2010 | By : Loiane | In : JSON, Spring
7
This tutorial will walk through how to configure Spring MVC to return a JSON object to client browser.
How to do it?
You can use json-lib-ext-spring. There are other libs, this is the one I found. If you know or use another one, please leave a comment with the library name.
Do not forget to download Json-lib and its dependencies.
Now you have to configure your XML files:
Create a views.xml file under WEB-INF folder and paste the following code into it:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation= "http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-2.5.xsd">
<bean name="jsonView" class="net.sf.json.spring.web.servlet.view.JsonView" />
</beans>
Add this config to you spring configuration file:
<!-- json --> <bean id="xmlFileViewResolver" class="org.springframework.web.servlet.view.XmlViewResolver"> <property name="location"> <value>/WEB-INF/views.xml</value> </property> <property name="order"> <value>1</value> </property> </bean>
Make sure to set the order if you are using any other view resolvers.
Now you just have to use “jsonView” as the viewname and the model will be converted to JSONbefore being sent back to the client:
return new ModelAndView("jsonView", modelMap);
Here is an example:
public ModelAndView getColumnsJson(HttpServletRequest request,
HttpServletResponse response) throws Exception {
Map<String,Object> modelMap = new HashMap<String,Object>(2);
modelMap.put("rows", service.generateColumns());
return new ModelAndView("jsonView", modelMap);
}
Happy coding!




I never use json-lib so I cannot tell about it. But I used flexjson and jackson.
flexjson: AFAIK, flexjson is only a one-way java object to json string transformation. If you use flexjson, your json string will also contain information that you rarely need (e.g. you json string will contain the class name of your java object).
jackson: 2-way java object json string. So you can send a javascript object to the server and have the information in that javascript object populated to the java object. And your json string doesn’t have the class name inside
How to create a json string from the browser? Just use JSON.stringify(a_js_object). Jackson is used in Spring 3.0. So I pick jackson over json-lib (I think Spring chose the best json library for them).
prefer use XStream and jettison
.
Anyway good article .
Rgds,
AR
Very good article. Thanks for taking the time for doing this.
Hi,
May I know, what does the
[modelMap.put("rows", service.generateColumns());]
service.generateColumns() looks like?
There is also GSON library,http://code.google.com/p/google-gson/ which give you more control for (de)serialization of objects (it help to tune json manipulation if you use inheritance etc.) Personally I prefer this library.
Thanks for this post I was able to fix my problem using your post. One question regarding this why cant we directly add the jsonView bean in the main configuration file ?
Excellent. I was looking for something to simple to get started. Do you have the complete project for distribution? Thanks.