In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article focuses on "what are the differences between the use of @ RequestParam and @ RequestBody". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let the editor take you to learn "what are the differences between @ RequestParam and @ RequestBody"?
@ RequestParam compares @ RequestBody with @ RequestParam
Used to process Content-Type: content encoded for application/x-www-form-urlencoded.
(in Http protocol, if Content-Type is not specified, the parameter passed by default is application/x-www-form-urlencoded type)
RequestParam can accept attributes of simple types as well as object types.
The essence is to convert the Key-Value parameter Map in Request.getParameter () into parameter receiving objects or fields by using the transformation mechanism ConversionService of Spring.
In the request of Content-Type: application/x-www-form-urlencoded
The value of queryString in get mode and the value of body data in post mode will be accepted by Servlet and converted to the Request.getParameter () parameter set, so @ RequestParam can get it.
Code example:
@ GetMapping ("/ test-sentinel-resource") public String testSentinelResource (@ RequestParam (required = false) String a) {if (StringUtils.isBlank (a)) {throw new IllegalArgumentException ("a cannot be blank.");} return a;}
Parameter a can be nullable when required= false, and cannot be nullable when required= true.
@ RequestBody
It is generally used to deal with data in a non-Content-Type: application/x-www-form-urlencoded encoding format.
In the GET request, @ RequestBody is not applicable because there is no HttpEntity.
In a POST request, the parameters passed through HttpEntity must declare the type of data in the request header Content-Type,SpringMVC by using HttpMessageConverters configured by HandlerAdapter
Parse the data in the HttpEntity and bind it to the appropriate bean.
For data of type application/json, you can use the annotation @ RequestBody to transfer all the json data in body to the backend, and then parse it.
In the GET request, @ RequestBody is not applicable because there is no HttpEntity.
In a POST request, the parameters passed through HttpEntity must declare the type of data in the request header Content-Type,SpringMVC by using the
HttpMessageConverters configured by HandlerAdapter to parse the data in HttpEntity, and then bind to the corresponding bean.
@ PostMapping ("/ create/user") public UserInfo createUser (@ RequestBody CreateUserRequest request) {return userService.createUser (request);}
A brief summary:
1. Form-data, x-www-form-urlencoded: @ RequestBody is not allowed
2. The application/json:json string part can be used in @ RequestBody;url? The following parameters can be used with @ RequestParam
3. @ RequestBody annotation cannot be used in get request
The difference between @ requestBody and @ requestparam;@requestBody: first, explain the difference between @ requestBody and @ requestParam
The RequestParam annotation of spring receives parameters from the requestHeader, the request header. Are used to get the dynamic parameters in the request path (url). That is, in url, the format is xxx?username=123&password=456. Features and @ pathvarible.
The parameters received by the RequestBody annotation come from the requestBody, that is, the request body.
Knowledge points:
II. Content-Type
Content type, which generally refers to the Content-Type that exists in the web page, is used to define the type of network file and the coding of the web page, and to determine what form and encoding the browser will read the file. This is why you often see a file or a picture downloaded when you click on some Asp pages.
3. Question: when will @ requestBody write or not?
Look at the phenomenon as follows: it is found that there are several ways to write ajaxDate.
(1) directly write the name and pass the parameter. As follows:
Print ajaxDate in the following format:
Although the printed result is a json object, it will eventually be converted to key1=value1&key2=value2 format and submitted to the background without adding @ requestBody.
(2) serializeble instantiation form
The print result is as follows:
The form format uses the default ContentType type application/x-www-form-urlencoded, and the format is submitted to the background as key1=value1&key2=value2, without the need for @ requestBody.
(3) after contentType conversion, you need to add @ requestBody,controller to receive the data.
The ajaxDate print results are as follows:
Post and get have the same function.
The foreground converts ajaxData into a json string, and the attribute annotated by @ RequestBody must be added in the background to customize the mapping to the value.
Why some need to add @ requestBody and some don't. The difference between adding and not adding is as follows:
Use @ requestBody. When the request content_type is of type application/json and the data type is json, the json format is as follows: {"aaa": "111"," bbb ":" 222"}
Do not use @ requestBody. When the request content_type is: application/x-www-form-urlencoded or multipart/form-data, the data format is aaa=111&bbb=222.
JQuery's $.ajax (url, [settings])
1. The default value of ContentType is: application/x-www-form-urlencoded; charset=UTF-8 this format is the form submission format, and the data is in key1=value1&key2=value2 format. Figure 2 serrializeble, using only the default contentType type.
two。 Although the data attribute value of ajax is in the format: {key1:value1,key2:value2}, it will eventually be submitted to the background in key1=value1&key2=value2 format.
Figure 2, although the printed result is not in & format, it will be converted.
3. If ajax wants to interact with springmvc and use the format of key1=value1&key2=value2, the background springmvc only needs to define objects or parameters and will automatically map.
4. If the parameter of springmvc is annotated with @ RequestBody (receives data in the format of json** string * *), ajax must convert the value of the date attribute to a json string, not a json object (js object, which is automatically converted to key=value). Also, change the value of contentType to: application/json; charset=UTF-8 so that attributes annotated with @ RequestBody can be customized to map to values.
5. When you use the multipart/form-data type when uploading pictures or files, the data is automatically mapped without adding any comments.
At this point, I believe you have a deeper understanding of "what is the difference between @ RequestParam and @ RequestBody". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.