@RequestBody和@RequestParam有什么区别? [英] What is difference between @RequestBody and @RequestParam?

查看:528
本文介绍了@RequestBody和@RequestParam有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了Spring文档以了解@RequestBody,他们给出了以下解释:

I have gone through the Spring documentation to know about @RequestBody, and they have given the following explanation:

@RequestBody方法参数注释指示方法参数应绑定到HTTP请求正文的值.例如:

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);
}

使用HttpMessageConverter将请求主体转换为方法参数. HttpMessageConverter负责从HTTP请求消息转换为对象,并从对象转换为HTTP响应主体.

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支持使用DefaultAnnotationHandlerMappingAnnotationMethodHandlerAdapter的基于注释的处理.在Spring 3.0中,AnnotationMethodHandlerAdapter进行了扩展以支持@RequestBody,并且默认情况下注册了以下HttpMessageConverter:

DispatcherServlet supports annotation based processing using the DefaultAnnotationHandlerMapping and AnnotationMethodHandlerAdapter. In Spring 3.0 the AnnotationMethodHandlerAdapter is extended to support the @RequestBody and has the following HttpMessageConverters registered by default:

...

但是我的困惑是他们在文档中写的句子是

but my confusion is the sentence they have written in the doc that is

@RequestBody方法参数注释指示方法参数应绑定到HTTP请求正文的值.

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?

spring doc中的@RequestParam定义是

The @RequestParam definition in spring doc is

注释,指示方法参数应绑定到Web请求参数.在ServletPortlet环境中支持带注释的处理程序方法.

Annotation which indicates that a method parameter should be bound to a web request parameter. Supported for annotated handler methods in Servlet and Portlet environments.

我对他们变得困惑.请帮我举个例子,说明它们之间的区别.

I have become confused between them. Please, help me with an example on how they are different from each other.

推荐答案

@RequestParam 带注释的参数链接到特定的Servlet请求参数.参数值将转换为声明的方法参数类型. 此注释表明方法参数应绑定到Web请求参数.

@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.

例如,Spring RequestParam的角度请求如下所示:

$http.post('http://localhost:7777/scan/l/register?username="Johny"&password="123123"&auth=true')
      .success(function (data, status, headers, config) {
                        ...
                    })

带有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 注释的参数链接到HTTP请求正文.使用HttpMessageConverters将参数值转换为声明的方法参数类型. 此注释表明方法参数应绑定到Web请求的正文.

@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.

例如,Spring RequestBody的角度请求如下所示:

$scope.user = {
            username: "foo",
            auth: true,
            password: "bar"
        };    
$http.post('http://localhost:7777/scan/l/register', $scope.user).
                        success(function (data, status, headers, config) {
                            ...
                        })

带有RequestBody的端点:

@RequestMapping(method = RequestMethod.POST, produces = "application/json", 
                value = "/register")
public Map<String, String> register(Model uiModel,
                                    @RequestBody User user,
                                    HttpServletRequest httpServletRequest) {... 

希望这会有所帮助.

这篇关于@RequestBody和@RequestParam有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆