Forward 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 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
  1. @RequestMapping (value="/response", Method=requestmethod.get)
  2. Public class Responsecontroller {
  3. Http://127.0.0.1:8010/response/annotation
  4. @RequestMapping ("/annotation")
  5. public @ResponseBody String responsebody () {
  6. return "the String responsebody";
  7. }
  8. @RequestMapping ("/charset/accept")
  9. public @ResponseBody String Responseacceptheadercharset () {
  10. return "\u3053\u3093\u306b\u3061\u306f\u4e16\u754c\uff01 (\" Hello world!\ "in Japanese)";
  11. }
  12. Http://127.0.0.1:8010/response/charset/produce
  13. @RequestMapping (value="/charset/produce", produces="Text/plain;charset=utf-8")
  14. public @ResponseBody String Responseproducesconditioncharset () {
  15. return "\u3053\u3093\u306b\u3061\u306f\u4e16\u754c\uff01 (\" Hello world!\ "in Japanese)";
  16. }
  17. Http://127.0.0.1:8010/response/entity/status
  18. @RequestMapping ("/entity/status")
  19. Public responseentity<string> Responseentitystatuscode () {
  20. return new responseentity<string> ("The String responsebody with custom status code (403 Forbidden)",
  21. Httpstatus.forbidden);
  22. }
  23. Http://127.0.0.1:8010/response/entity/headers
  24. @RequestMapping ("/entity/headers")
  25. Public responseentity<string> responseentitycustomheaders () {
  26. Httpheaders headers = new Httpheaders ();
  27. Headers.setcontenttype (Mediatype.text_plain);
  28. return new responseentity<string> ("The String responsebody with the custom header content-type=text/ Plain ",
  29. headers, Httpstatus.ok);
  30. }
  31. }






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

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.