In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly shows you "how to solve the problem of the failure of POST method to pass parameters to @ RequestBody". The content is simple and clear. I hope it can help you solve your doubts. Let me lead you to study and learn this article "how to solve the problem of POST method failing to pass parameters to @ RequestBody".
POST method failed to pass parameters to @ RequestBody
When passing parameters to springMVC through ajax, the json string is usually passed through the post method. In this case, the backend should parse the json string through the @ RequestBody annotation together with the message converter configured in springMVC.
Because the json string in the post method is usually a string converted from a front-end json object, the back-end @ RequestBody can be followed by a HashMap type to receive all parameters.
In this way, a large number of parameters can be non-explicitly transmitted to the back end, and it has nothing to do with the specific parameters, so it is convenient to expand the parameters in the future. in the case of uncertain parameters, this way is uncoupled with the specific parameters. But because the simple process actually goes through a lot of SpringMVC operations, it often encounters a variety of strange problems.
It can be seen that the json string in the body of the post method request cannot be successfully converted into @ RequestBody annotated HashMap objects through the MessageConverter (usually MappingJackson2HttpMessageConverter) configured in SpringMVC.
At this time, you can check the content-type request header of the ajax request, because when configuring the MessageConverter message converter, you need to configure the request body type it checks (content-type). By default, the ajax of the browser will add the request header of content-type:application/x-www-form-urlencoded, but some browsers may sometimes send content-type:application/x-www-form-urlencoded. Charset:utf-8 goes to the backend, causing MappingJackson2HttpMessageConverter to miss its check. At this time, SpringMVC will use other MessageConverter to parse the contents of the request body, which is sure to fail.
If you encounter this situation, a better solution may be to force contentType:application/json in the post request of ajax, and add application/json and application/json;charset=utf-8 MediaType (MimeType) to the checklist of MappingJackson2HttpMessageConverter objects.
This ensures that the json string in the request body of the post method is checked and parsed by the MappingJackson2HttpMessageConverter object.
@ RequestBody parameter mapping is not up, @ RequestBody reported an error
Recently, the code encountered a small problem in springmvc, which is a very basic thing. After my colleagues asked me, I was a little confused and found some information on the Internet. Make a note of the springmvc annotation @ requestbody to pass parameters to map an object.
To get back to the point, here's the question.
Use postMan to send an httppost request:
In the end, it was wrong.
Reason for reporting an error
The following is not selected when sending a post request using postMan:
That is, what we need to pay attention to is as follows:
1. The front end submits a lot of parameters when doing the save operation, and when the front end code is submitted, the content-type is changed to application/json, and the background is also configured with a son type httpmessageConverter for spring4.
@ data type bound by RequsetBody:
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 parses the data in the HttpEntity by using the HttpMessageConverters configured by HandlerAdapter, and then binds to the corresponding bean.
1. The number of matches between the front and back end parameters of @ RequestBody must not be small. The field name should be the same.
2. Check the version of the configuration file spring. The adapter of the corresponding version of spring4 corresponds to:
Please use org.springframework.http.converter.json.MappingJacksonHttp2MessageConverter, the selected configuration is the jackson2.X version, which is used to parse json data.
The corresponding version of spring 3.x:
Org.springframework.http.converter.json.MappingJacksonHttpMessageConverter
This configuration uses the jackson1.X version
3, the data type Content-Type must be declared in the request header
4. Do not capitalize the first letters of front-end json data and back-end json data, do not map the first letters in uppercase, and name them in hump format.
The above is all the contents of this article entitled "how to solve the problem of POST method failing to pass parameters to @ RequestBody". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.