Three methods of submitting form data with spring boot resttemplate

In the design of rest interface, using resttemplate for interface testing is a common method, but in the process of use, due to its many method parameters, many students confuse the difference between form submission and payload submission, and the interface design is different from the submission method used by traditional browsers. There are often various errors, such as 405 error, Or the submitted data is not available at all. The error example is as follows:

1. Submit with exchange method

Exchange can execute both post and get methods, so it is most widely used. The usage methods are as follows:

2. Submit with postforentity

Postforentity is a simplification of exchange. It only needs to reduce httpmethod Post parameters, as follows:

3. Differences between form submission and payload submission

In the method parameters of the controller, if the "@ modeldistribution" is changed to the "@ requestbody" annotation, the submission method is payload. The code example is as follows:

Once again, do not add "@ modelattribute" again. Because it has a high priority, the system will parse the submitted content in the form.

For payload mode, the submitted content must be string, and the header must be set to "application / JSON". The example is as follows:

If the content is not submitted in string mode, the following error must occur:

Finally, it should be emphasized that the request parameters cannot be obtained through @ requestbody. If the above server code is changed to the following format, the data will certainly not be obtained, but the form submission is the opposite.

4. Structure of httpentity

Httpentity is the encapsulation of HTTP request, including two parts: header and body. Header is used to set the request header and body is used to set the request body. Therefore, its constructor is as follows:

5. Httpentity and urivariables

In the use of resttemplate, httpentity is used to pass specific parameter values, while urivariables is used to format HTTP addresses instead of address parameters. The correct usage is as follows:

6. Description of httpmessageconverter

In many online examples, I found that many people have added custom httpmessageconverter to handle payload submission, as follows:

Then, after checking the source code and debugging, I found that resttemplate has built-in seven kinds of httpmessageconverters, as follows:

1. org. springframework. http. converter. ByteArrayHttpMessageConverter 2. org. springframework. http. converter. StringHttpMessageConverter 3. org. springframework. http. converter. ResourceHttpMessageConverter 4. org. springframework. http. converter. xml. sourceHttpMessageConverter 5. org. springframework. http. converter. support. AllEncompassingFormHttpMessageConverter 6. org. springframework. http. converter. xml. Jaxb2RootElementHttpMessageConverter 7. org. springframework. http. converter. json. MappingJackson2HttpMessageConverter “`

conclusion

Resttemplate can greatly simplify the difficulty of submitting form data and has the function of automatically converting JSON data. However, only by understanding the composition structure of httpentity (header and body) and the difference between httpentity and urivariables can we really master its usage.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>