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 pointing directly to a specific view;
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 results directly,
and responseentity
You can define the returned httpheaders and Httpstatus, see the last two pictures of the article
[Java]View PlainCopy
- @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\u3093\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 code (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);
- }
- }
Forward SPRING MVC3.2 Case Study--spring MVC3 @responsebody and Responseentity