Pring How MVC returns JSON data, there are probably several:
Spring MVC 3.0 method to return JSON data
1. Direct PrintWriter output
2. Use JSP view
3. Using Spring's built-in support
The following are the configurations before 3.1:
<bean class= "Org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" >
< Property name= "Messageconverters" >
<list>
<bean
class= " Org.springframework.http.converter.json.MappingJacksonHttpMessageConverter "/>
</list>
</ Property>
</bean>
here's the point.
The following configuration is 3.1 :
Use requestmappinghandlermapping and requestmappinghandleradapter after spring MVC 3.1
instead of the original defaultmethodhandlermapping and Annotationmethodhandleradapter,
So, 3.1 of the return JSON configuration can be configured with the following:
<bean
class= "Org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"
p: Ignoredefaultmodelonredirect= "true" >
<property name= "Messageconverters" >
<list>
< Bean class= "Org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>
</list>
</property>
</bean>
Add in Web-inf/lib
Jackson-mapper-asl-1.6.4.jar
Jackson-core-asl-1.6.4.jar
Used in Controller: @ResponseBody callout method, which returns a Java object (supports complex objects).
Such as:
Public @ResponseBody user getuser (user u) {return
u;
}
Mappingjacksonhttpmessageconverter converts an object to JSON output
Note : When <mvc:annotation-driven/> is used in Springmvc-servlet.xml, the default is injected if it is 3.1 Annotationmethodhandleradapter, after 3.1, the default injection requestmappinghandleradapter only add the jar package mentioned above!
Note: By default, Mappingjacksonhttpmessageconverter sets the content to Application/json, and returns a prompt download under IE9, which allows you to manually specify header information as " Text/html ", or" */* "(all, not sure to set this)
if it is manually injected Requestmappinghandleradapter can be set this way
<bean
class= "Org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"
p: Ignoredefaultmodelonredirect= "true" >
<property name= "Messageconverters" >
<list>
<bean
class= "Org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"
p: supportedmediatypes= "*/*"/>
</list>
</property>
</bean>
If you use the Mvc:annotation-driven tag, you can inject information like this
<mvc:annotation-driven>
<mvc:message-converters register-defaults= "true" >
<bean
class= "Org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"
p:supportedmediatypes= " */* "/>
</mvc:message-converters>
</mvc:annotation-driven>
4. Custom
Configure Beannameviewresolver
<bean class= "Org.springframework.web.servlet.view.BeanNameViewResolver" >
<property name= "Order" value= "1"/> </bean> <bean id= "Jsonview"
test. Testjsonview "/>
Implement Testjsonview public
class Testjsonview extends Abstractview {
@Override
protected void Rendermergedoutputmodel (map<string, object> model, httpservletrequest request,
HttpServletResponse Response) throws Exception {
jsonbuilder jb = new Jsonbuilder ();
PrintWriter out = Response.getwriter ();
Out.print (Jb.encode (model));
}
Controller can be written in this way:
modelandview mav = new Modelandview ("Jsonview");
Mav.addobject ... return MAV;
Source: http://yjflfliulei.iteye.com/blog/1755134