SPRING MVC3.2 Case Study--spring MVC3 @responsebody and Responseentity

Source: Internet
Author: User

In the traditional development process, our control controll layer usually needs to turn to a JSP view, but with the rise of WEB2.0 related technologies, we often just need to return the data, rather than a JSP page.

SPRING MVC3 's @responsebody enables the controller to return data directly, rather than directly to a specific view, and through Messageconverter and produces (such as produces= "Text/plain Charset=utf-8 ") can return data in various formats (Xml,json,rss,text, byte stream, etc.), this chapter only describes the simplest use;

See Code:

@ResponseBody can return the results directly, and responseentity can define the returned httpheaders and Httpstatus, as shown in the last two pictures of the article

@RequestMapping (value= "/response", Method=requestmethod.get) public class Responsecontroller {//http://  127.0.0.1:8010/response/annotation @RequestMapping ("/annotation") public @ResponseBody String Responsebody () {return
	"The String responsebody"; } @RequestMapping ("/charset/accept") public @ResponseBody String Responseacceptheadercharset () {return "\u3053\u309
	3\u306b\u3061\u306f\u4e16\u754c\uff01 (\ "Hello world!\" in Japanese) "; }//http://127.0.0.1:8010/response/charset/produce @RequestMapping (value= "/charset/produce", produces= "Text/plain ; Charset=utf-8 ") Public @ResponseBody String Responseproducesconditioncharset () {return" \u3053\u3093\u306b\u3061\
	U306f\u4e16\u754c\uff01 (\ "Hello world!\" in Japanese) "; }//http://127.0.0.1:8010/response/entity/status @RequestMapping ("/entity/status") public responseentity<string > Responseentitystatuscode () {return new responseentity<string> ("The String responsebody with custom status Co
De (403 Forbidden) ",				Httpstatus.forbidden); }//http://127.0.0.1:8010/response/entity/headers @RequestMapping ("/entity/headers") public responseentity<
		String> responseentitycustomheaders () {httpheaders headers = new Httpheaders ();
		Headers.setcontenttype (Mediatype.text_plain);  return new Responseentity<string> ("The String responsebody with the custom header Content-type=text/plain", headers,
	Httpstatus.ok); }

}







Size: 112.8 KB size: 111.4 KB size: 46.8 KB View picture attachments

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.