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