The difference between @RequestParam and @RequestBody of spring boot

Respect originality: https://blog.csdn.net/u013306545/article/details/79071683

1. Problem description

      Since the front and back ends of the project are separated, spring boot is used in the background, which is made into microservices and only exposes interfaces. The interface design style is restful. In the get request, an error will be reported when the annotation of the received parameter in the background is RequestBody; in the post request, an error will also be reported when the annotation of the received parameter in the background is RequestParam.

Second, the cause of the problem

     Since the parameters received by spring's RequestParam annotation come from the requestHeader, that is, the request header, that is, in the url, the format is xxx?username=123&password=456, and the parameters received by the RequestBody annotation are from the requestBody, that is, the request body middle.

3. Solutions

      Therefore, in summary, if it is a get request, the annotation of the background received parameter should be RequestParam, and if it is a post request, the annotation of the background received parameter is RequestBody. Attached are two examples, screenshots are as follows:

      get request

  

post request

 

        In addition, there is another application scenario, when the interface specification is resultful style, for example: if you want to obtain the number of queries for the answer to this question under a certain id, the background needs to dynamically obtain parameters, which are annotated as @PathVariable, and The value in requestMapping should be value="/{id}/queryNum". The screenshot is as follows:

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325267898&siteId=291194637