preface

Recently, during the joint debugging with the front-end, the front-end transmission parameters could not be resolved. Finally, the problem was found to be the Content-Type. As a developer with more than a year of experience, it should not. So, in writing this article, I want to sort out what is involved in front and back end parameter passing.

A, the GET/POST/DELETE/PUT

1. Semantic differences

  • Get: Get data, idempotent.
  • Post: sends data, creates new content (database Insert)
  • Put: sends data and modifies data content (Update of database)
  • DELETE: deletes data

2. Differences in details

W3SCHOOL Get and Post

GET POST
Back button/refresh harmless The data will be resubmitted (browsers should inform users that the data will be resubmitted).
bookmarks Bookmark Do not bookmark
The cache Can be cached Can’t cache
The encoding type application/x-www-form-urlencoded Application/x – WWW – form – urlencoded or multipart/form – the data. Use multiple encoding for binary data.
history Parameters are retained in browser history. Parameters are not saved in browser history.
A limit on the length of data Yes. When sending data, the GET method adds data to the URL; URL length is limited (maximum LENGTH of URL is 2048 characters). Unlimited.
Restrictions on data types Only ASCII characters are allowed. There is no limit. Binary data is also allowed.
security GET is less secure than POST because the data sent is part of the URL. Never use GET! When sending passwords or other sensitive information. POST is more secure than GET because parameters are not saved in browser history or Web server logs.
visibility The data is visible to everyone in the URL. The data is not displayed in the URL.

3, the content-type

Tip:

The HTTP protocol uses a request/response model, in which an HTTP message consists of a starting line, one or more header fields, an empty line that only ends in the header field, and an optional message body.

HTTP header field includes four parts: general header, request header, response header and entity header. Content-type belongs to the entity header.

A GET request concatenates parameters to a URL without a Content-Type field. Post/PUT/DELETE requests have content-Type fields

Content-type represents the data type of entity data sent by the sender, indicating the MIME type of subsequent documents.

Common content-Types are:

  1. `application/x-www-form-urlencodedThis is just a regular text form that uses POST to pass data. Native browser forms, if encType is not set, will end up submitting data as Application/X-www-form-urlencoded.
  1. multipart/form-dataIn this case, the encType attribute of the form must be multipart/form-data.
  2. application/json, pass the data in json object format;
  3. text/xmlXML as the encoding method.

Spring MVC accepts annotations related to parameters

1. @ RequestParam

The @RequestParam annotation is used to receive parameters in the URL, mainly for GET requests.

Note:

If the parameters are concatenated to the Url in a Post request, they can also be received with the @requestParam annotation.

2, @ RequestBody

The @requestbody annotation is used to receive the parameters of the RequestBody. The content-type is application/json, which is mainly used for Post, put, or delete.

3. Without notes

The unannotated case is mainly used for application/ X-www-form-urlencoded, multipart/form-data, and is mainly used for Post/ PUT /delete.

Whether to annotate @contentType x-www-form-urlencoded form-data application/json
No @requestBody annotation You can receive You can receive Can’t receive
Add the @requestBody annotation Can’t receive Can’t receive You can receive

4, @ PathVariable

The @pathvariable is mainly used to bind placeholder parameters in urls to input parameters of controller processing methods.

/ * * * : 1 * * localhost: 8080 / pathVariable/these urls will perform this method And will be passed as a parameter to the id field * 1 * @ @ param id return * / @RequestMapping("/pathVariable/{id}") public String pathVariable(@PathVariable("id") String id){ return id; }Copy the code

conclusion

  • Get, POST, DELETE, and PUT are all based on THE HTTP protocol. The differences are mainly in packet formats.
  • Content-type Indicates the data type of entity data sent by the sendermultipart/form-dataapplication/x-www-form-urlencoded,`application/json,text/xml
  • @requestParam, @RequestBody and other annotations receive parameters based on the URL and Content-Type, regardless of the request method. For example, @requestParam can also be used for POST requests.

reference

It’s 2019 and ask the difference between GET and POST

Use @requestParam and @RequestBody flexibly

Form Data and Request Payload

The @RequestBody annotation in Spring and the normal HTTP method for passing values

\