Spring boot Get and post requests, and requestbody for JSON string processing

Source: Internet
Author: User

GET, post method, according to the request header Content-type value to judge:

    • application/x-www-form-urlencoded, Optional (that is, not necessary, because the data of this situation @requestparam, @ModelAttribute can also be processed, of course @requestbody can also handle);
    • Multipart/form-data, cannot be processed (i.e. using @requestbody cannot process data in this format);
    • Other formats must be (other formats include Application/json, Application/xml, etc.). Data in these formats must be handled using @requestbody);
Code:  [Java]View PlainCopy
  1. Package Com.example.controller;
  2. Import org.springframework.web.bind.annotation.GetMapping;
  3. Import org.springframework.web.bind.annotation.PathVariable;
  4. Import Org.springframework.web.bind.annotation.RequestBody;
  5. Import org.springframework.web.bind.annotation.RequestMapping;
  6. Import Org.springframework.web.bind.annotation.RequestMethod;
  7. Import Org.springframework.web.bind.annotation.RequestParam;
  8. Import Org.springframework.web.bind.annotation.RestController;
  9. Import Com.example.bean.RequestLoginBean;
  10. Import Com.example.response.BaseResponse;
  11. Import Com.google.gson.Gson;
  12. @RestController
  13. @RequestMapping (value = "/index")
  14. Public class Login {
  15. /** 
  16. * Index Home
  17. *
  18. * @return
  19. */
  20. @RequestMapping (value = "/home")
  21. Public String Home () {
  22. return "Index Home";
  23. }
  24. /** 
  25. * Get 1 parameters
  26. *
  27. * @param name
  28. * User Name
  29. * @return Return results
  30. */
  31. @GetMapping (value = "/{name}")
  32. Public String Index (@PathVariable string name) {
  33. return "Oh you is" + name + "<br> Nice to meet you"; It doesn't work, so just use the tags in the HTML .
  34. }
  35. /** 
  36. * Simple POST request
  37. *
  38. * @param name
  39. * @param pwd
  40. * @return
  41. */
  42. @RequestMapping (value = "/testpost", method = Requestmethod.post)
  43. Public String Testpost () {
  44. System.out.println ("Hello test post");
  45. return "OK";
  46. }
  47. /** 
  48. * Get two parameters at the same time
  49. *
  50. * @param name
  51. * User Name
  52. * @param pwd
  53. * Password
  54. * @return Return results
  55. */
  56. @GetMapping (value = "/login/{name}&{pwd}")
  57. Public String Login (@PathVariable string name, @PathVariable string pwd) {
  58. if (name.equals ("admin") && pwd.equals ("admin")) {
  59. return "Hello welcome admin";
  60. } Else {
  61. return "Oh sorry user name or password is wrong";
  62. }
  63. }
  64. /** 
  65. * Login via GET request
  66. *
  67. * @param name
  68. * @param pwd
  69. * @return
  70. */
  71. @RequestMapping (value = "/loginbyget", method = Requestmethod.get)
  72. Public string Loginbyget (@RequestParam (value = "Name", required = true) string name,
  73. @RequestParam (value = "pwd", required = true) String pwd) {
  74. return Login4return (name, PWD);
  75. }
  76. /** 
  77. * Login via POST request
  78. *
  79. * @param name
  80. * @param pwd
  81. * @return
  82. */
  83. @RequestMapping (value = "/loginbypost", method = Requestmethod.post)
  84. Public string Loginbypost (@RequestParam (value = "Name", required = true) string name,
  85. @RequestParam (value = "pwd", required = true) String pwd) {
  86. System.out.println ("Hello post");
  87. return Login4return (name, PWD);
  88. }
  89. /** 
  90. * The parameter is a Bean object. Spring automatically associates mappings for us
  91. * @param Loginbean
  92. * @return
  93. */
  94. @RequestMapping (value = "/loginbypost1", method = {requestmethod.post, requestmethod.get})
  95. Public String LoginByPost1 (Requestloginbean loginbean) {
  96. if (null! = Loginbean) {
  97. return Login4return (Loginbean.getname (), loginbean.getpwd ());
  98. } Else {
  99. return "error";
  100. }
  101. }
  102. /** 
  103. * The request content is a JSON string, and spring will automatically match him to our parameter bean, but add @requestbody annotations.
  104. *
  105. * @param name
  106. * @param pwd
  107. * @return
  108. */
  109. @RequestMapping (value = "/loginbypost2", method = {requestmethod.post, requestmethod.get})
  110. Public String loginByPost2 (@RequestBody requestloginbean loginbean) {
  111. if (null! = Loginbean) {
  112. return Login4return (Loginbean.getname (), loginbean.getpwd ());
  113. } Else {
  114. return "error";
  115. }
  116. }
  117. /** 
  118. * Ways to respond to logins
  119. *
  120. * @param name
  121. * User Name
  122. * @param pwd
  123. * Password
  124. * @return Return processing results
  125. */
  126. private String Login4return (string name, string pwd) {
  127. String result;
  128. Baseresponse response = new Baseresponse ();
  129. if (name.equals ("admin") && pwd.equals ("admin")) {
  130. result = "Hello welcome admin";
  131. Response.setstate (true);
  132. } Else {
  133. result = "Oh sorry user name or password is wrong";
  134. Response.setstate (false);
  135. }
  136. System.out.println ("received request, request result:" + result);
  137. return new Gson (). ToJson (response);
  138. }
  139. }


Spring boot Get and post requests, and requestbody for JSON string processing

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.