Spring MVC 3.1 annotation: @ResponseBody return JSON data __js

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.