The difference between spring Boot's @requestparam and @requestbody

Source: Internet
Author: User

Respect for the original: 790,716,831, problem description

Because the project is a front-end separation, the backend uses spring boot, which is made into microservices, exposing only the interface. Interface design style for the restful style, in the GET request, the background receive parameter annotation is requestbody error, in the POST request, the background receives the parameter annotation is Requestparam will also error.

Second, the cause of the problem

Since spring's requestparam annotations receive parameters from Requestheader, which is the request header, which is in the URL, the format is xxx?username=123&password= 456, while the requestbody annotation receives the parameter from the Requestbody, which is the request body.

Third, the solution

Therefore, in summary, if a GET request, the background receive parameter annotation should be requestparam, if it is a POST request, the background receive parameter annotation is requestbody. Attached are two examples, as follows:

GET request

POST request

In addition, there is an application scenario, the interface specification for the resultful style, for example: If you want to get an ID under the question of the answer to the number of queries, then the background needs to dynamically get the parameters, the annotation is @pathvariable, and the value in requestmapping should be value= "/{id}/querynum", as follows:

The difference between spring Boot's @requestparam and @requestbody

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.