Home > Java > javaTutorial > body text

The difference between requestbody and requestparam

(*-*)浩
Release: 2020-01-09 10:22:12
Original
9625 people have browsed it

The difference between requestbody and requestparam

##@RequestParam                                                                                                                                                                                                                                                                        -www-form-urlencoded encoded content. (In the Http protocol, if Content-Type is not specified, the parameter passed by default is the application/x-www-form-urlencoded type)

@RequestBody


Process the data passed by HttpEntity, generally used to process data in non-Content-Type: application/x-www-form-urlencoded encoding format.

•In the GET request, because there is no HttpEntity, @RequestBody does not apply.

•In POST requests, the parameters passed through HttpEntity must declare the data type Content-Type in the request header. SpringMVC parses the data in HttpEntity by using the HttpMessageConverters configured with

HandlerAdapter , and then bind to the corresponding bean.

Summary

•In GET requests, @RequestBody cannot be used.

•In POST requests, you can use @RequestBody and @RequestParam, but if you use @RequestBody, the configuration of parameter conversion must be unified.

For example, in the HttpMessageConverters processing stack configured in SpringMVC, specify the json conversion format. For example, if Date is converted to 'yyyy-MM-dd', then the fields contained in the parameter receiving object If it is a Date type, the client can only pass the year, month and day format, but not the hours, minutes and seconds. Because different interfaces, their parameters may have different format requirements for time parameters, so doing so will make the client calling colleagues a little confused about the format of the parameters, so the scalability is not high.

If you use @RequestParam to accept parameters, you can set @DateFormat in the model that accepts parameters to specify the format of the time parameters you need to accept.

In addition, the parameters accepted by using @RequestBody will not be converted by Servlet and placed in the Param parameter set of the request object. @RequestParam is acceptable.

For more Java-related technical articles, please visit the

java tutorial

column to learn!

The above is the detailed content of The difference between requestbody and requestparam. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template