Correct use of @RequestBody in Springboot

Source: Internet
Author: User

Correct use of @RequestBody in Springboot

Recently received a job to leave a colleague, took over the project was built with Springboot, which saw this writing:

[Java]View PlainCopy
  1. @RequestMapping ("Dothis")
  2. Public String Dothis (HttpServletRequest request,
  3. @RequestParam ("id") Long ID, //user ID
  4. @RequestParam ("Back_url") String Back_url, //callback address
  5. @RequestBody testentity json_data //JSON data, for Java entity classes
  6. ){//...  


This is a request mapping method, and then enter url:http://127.0.0.1:8080/test/dothis?id=1&back_url=url&json_data={"code" with the browser: 2, " Message ":" Test "}

In this method, use @requestparam to get the parameters and then use @requestbody to convert the JSON-formatted parameters to Java types

Error found at run time: Required request body is missing

The use of the @RequestBody needs to load Mappingjackson2httpmessageconverter, but Springboot's official documentation mentions that this is already loaded by default, And the JSON string and the JavaBean have no written errors.

So consider the question of requesting content-type because there is no way to define content-type by using a browser to enter a URL, so spring cannot find the request body

To prove the idea, write a request class yourself:

[Java]View PlainCopy
  1. String Add_url = "Http://127.0.0.1:8080/test/doThis";
  2. URL url = new URL (add_url);
  3. HttpURLConnection connection = (httpurlconnection) url.openconnection ();
  4. Connection.setdoinput (true);
  5. Connection.setdooutput (true);
  6. Connection.setrequestmethod ("POST");
  7. Connection.setusecaches (false);
  8. Connection.setinstancefollowredirects (true);
  9. Connection.setrequestproperty ("Content-type","Application/json");
  10. Connection.connect ();
  11. DataOutputStream out = new DataOutputStream (Connection.getoutputstream ());
  12. Jsonobject obj = new Jsonobject ();
  13. Obj.put ("code",-1002);
  14. Obj.put ("message", "msg");
  15. Out.writebytes (Obj.tostring ());
  16. Out.flush ();
  17. Out.close ();

Request or failure, after debugging, find that all @requestparam annotations need to be removed to succeed

Summarize:

1, @RequestBody need to parse all the request parameters as JSON, therefore, cannot contain key=value such a notation in the request URL, all request parameters are a JSON

2, directly through the browser input URL, @RequestBody get not JSON object, need to use Java programming or AJAX-based method request, set Content-type to Application/json

Correct use of @RequestBody in Springboot

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.