@RequestBody 和@RequestParam 有什么区别? [英] What is difference between @RequestBody and @RequestParam?
问题描述
我已经阅读了 Spring 文档以了解 @RequestBody
,他们给出了以下解释:
@RequestBody
方法参数注解表示方法参数应该绑定到 HTTP 请求正文的值上.例如:
@RequestMapping(value = "/something", method = RequestMethod.PUT)public void handle(@RequestBody String body, Writer writer) 抛出 IOException {writer.write(body);}
<块引用>
您可以使用 HttpMessageConverter
将请求正文转换为方法参数.HttpMessageConverter
负责将 HTTP 请求消息转换为对象,以及将对象转换为 HTTP 响应体.
DispatcherServlet
使用 DefaultAnnotationHandlerMapping
和 AnnotationMethodHandlerAdapter
支持基于注解的处理.在 Spring 3.0 中,AnnotationMethodHandlerAdapter
被扩展为支持 @RequestBody
并且默认注册了以下 HttpMessageConverter
:
...
但我的困惑是他们在文档中写的句子是
<块引用>@RequestBody 方法参数注解表示方法参数应该绑定到 HTTP 请求正文的值上.
他们这是什么意思?谁能给我举个例子?
spring 文档中的@RequestParam
定义是
指示方法参数应绑定到 Web 请求参数的注释.支持 Servlet
和 Portlet
环境中带注释的处理程序方法.
我对他们感到困惑.请帮我举例说明它们之间的区别.
@RequestParam
带注释的参数链接到特定的 Servlet 请求参数.参数值被转换为声明的方法参数类型.此注解表示方法参数应该绑定到 Web 请求参数.
例如,Spring RequestParam(s) 的 Angular 请求将如下所示:
$http.post('http://localhost:7777/scan/l/register?username="Johny"&password="123123"&auth=true').success(功能(数据,状态,标题,配置){...})
带有 RequestParam 的端点:
@RequestMapping(method = RequestMethod.POST, value = "/register")公共地图<字符串,字符串>注册(模型uiModel,@RequestParam 字符串用户名,@RequestParam 字符串密码,@RequestParam 布尔身份验证,HttpServletRequest httpServletRequest) {...
@RequestBody
带注释的参数链接到 HTTP 请求正文.使用 HttpMessageConverters 将参数值转换为声明的方法参数类型.这个注解表明一个方法参数应该绑定到 Web 请求的主体.
例如 Spring RequestBody 的 Angular 请求看起来像这样:
$scope.user = {用户名:foo",身份:真实,密码:酒吧"};$http.post('http://localhost:7777/scan/l/register', $scope.user).成功(功能(数据,状态,标题,配置){...})
带有 RequestBody 的端点:
@RequestMapping(method = RequestMethod.POST, products = "application/json",值 = "/注册")公共地图<字符串,字符串>注册(模型uiModel,@RequestBody 用户用户,HttpServletRequest httpServletRequest) {...
希望这会有所帮助.
I have gone through the Spring documentation to know about @RequestBody
, and they have given the following explanation:
The
@RequestBody
method parameter annotation indicates that a method parameter should be bound to the value of the HTTP request body. For example:
@RequestMapping(value = "/something", method = RequestMethod.PUT)
public void handle(@RequestBody String body, Writer writer) throws IOException {
writer.write(body);
}
You convert the request body to the method argument by using an
HttpMessageConverter
.HttpMessageConverter
is responsible for converting from the HTTP request message to an object and converting from an object to the HTTP response body.
DispatcherServlet
supports annotation based processing using theDefaultAnnotationHandlerMapping
andAnnotationMethodHandlerAdapter
. In Spring 3.0 theAnnotationMethodHandlerAdapter
is extended to support the@RequestBody
and has the followingHttpMessageConverter
s registered by default:...
but my confusion is the sentence they have written in the doc that is
The @RequestBody method parameter annotation indicates that a method parameter should be bound to the value of the HTTP request body.
What do they mean by that? Can anyone provide me an example?
The @RequestParam
definition in spring doc is
Annotation which indicates that a method parameter should be bound to a web request parameter. Supported for annotated handler methods in
Servlet
andPortlet
environments.
I have become confused between them. Please, help me with an example on how they are different from each other.
@RequestParam
annotated parameters get linked to specific Servlet request parameters. Parameter values are converted to the declared method argument type.
This annotation indicates that a method parameter should be bound to a web request parameter.
For example Angular request for Spring RequestParam(s) would look like that:
$http.post('http://localhost:7777/scan/l/register?username="Johny"&password="123123"&auth=true')
.success(function (data, status, headers, config) {
...
})
Endpoint with RequestParam:
@RequestMapping(method = RequestMethod.POST, value = "/register")
public Map<String, String> register(Model uiModel,
@RequestParam String username,
@RequestParam String password,
@RequestParam boolean auth,
HttpServletRequest httpServletRequest) {...
@RequestBody
annotated parameters get linked to the HTTP request body. Parameter values are converted to the declared method argument type using HttpMessageConverters.
This annotation indicates a method parameter should be bound to the body of the web request.
For example Angular request for Spring RequestBody would look like that:
$scope.user = {
username: "foo",
auth: true,
password: "bar"
};
$http.post('http://localhost:7777/scan/l/register', $scope.user).
success(function (data, status, headers, config) {
...
})
Endpoint with RequestBody:
@RequestMapping(method = RequestMethod.POST, produces = "application/json",
value = "/register")
public Map<String, String> register(Model uiModel,
@RequestBody User user,
HttpServletRequest httpServletRequest) {...
Hope this helps.
这篇关于@RequestBody 和@RequestParam 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!