When submitting X-www-form-urlencoded using Sprint Boot, we first need to know what x-www-form-urlencoded is.

application/x-www-form-urlencoded

This is probably the most common way to POST data. Browser native forms, if encType is not set, will end up submitting data as Application/X-www-form-urlencoded.

The browser converts the content of the form into a URL and submits it to the background for processing. The way to convert is to use the K=V pair.

Different K equals V are separated by ampersand.

For example:

POST /test HTTP/1.1
Host: foo.example
Content-Type: application/x-www-form-urlencoded
Content-Length: 27

field1=value1&field2=value2
Copy the code

You can just put the above string on the end of your API, and then POST the request, you need to put one between the above string and the URL, right? Number.

The conversion process is as follows:

Suppose you have a form in which the values obtained are:

field1=value1

field2=value2

You need to submit a URL for api.example.com/re/request such things.

If you submit with application/ X-www-form-urlencoded, the browser will first convert the data in the form to field1= Value1 &field2=value2, with & separated in the middle.

And then submit to the address: api.example.com/re/request?… The URL.

This is the default submission method for the form, and the submitted characters will be encoded. If you submit Chinese characters, you might see something like %UER, because Chinese characters are encoded.

POSTMAN set

Before using this type of submission, you can set it up in POSTMAN.

You need to select the submission method in POSTMAN and set the required parameters in the parameters section below and click Send Submit.

Spring Boot Controller

The Spring Boot Controller needs to be set up to use MultiValueMap.

After looking at the source code, you will know that MultiValueMap is an interface that extends a Map and is then used to store multiple values.

The code for a Controller is as follows:

@PostMapping("/soldm") public ResponseEntity<? > searchUsers(@RequestBody MultiValueMap< String, String > values) { logger.debug("K=V Map - {}" , values); REListing reListing= listingService.getREListingById(); return new ResponseEntity<REListing>(reListing, HttpStatus.OK); }Copy the code

After the RequestBody is set to MultiValueMap, you can get the data submitted by the POST.

A test run

View log after test run:

The 2021-01-27 10:52:38. 25176-782 the DEBUG [nio - 8080 - exec - 4] C.O.E.S.C ontroller. RealEstateController: K=V Map - {field1=[value1], field2=[value1]}Copy the code

From here, you can debug the uploaded data and whether the correct parameters are set in the MAP.

www.ossez.com/t/spring-bo…