@RequestBody和@RequestParam之间有什么区别?

Man*_*ngh 54 spring spring-mvc servlet-3.0 http-request-parameters spring-boot

我已经通过Spring文档了解@RequestBody,并且他们给出了以下解释:

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

@RequestMapping(value = "/something", method = RequestMethod.PUT)
public void handle(@RequestBody String body, Writer writer) throws IOException {
  writer.write(body);
}
Run Code Online (Sandbox Code Playgroud)

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

DispatcherServlet支持使用DefaultAnnotationHandlerMapping和支持基于注释的处理AnnotationMethodHandlerAdapter.在Spring 3.0中,AnnotationMethodHandlerAdapter扩展为支持@RequestBodyHttpMessageConverter默认注册以下s:

...

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

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

那是什么意思?任何人都可以举个例子吗?

@RequestParamspring doc中的定义是

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

我在他们之间感到困惑.请帮助我举一个关于它们彼此不同的例子.

Pat*_*ego 73

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

例如,对Spring RequestParam的Angular请求看起来像这样:

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

使用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) {...
Run Code Online (Sandbox Code Playgroud)

@RequestBody带注释的参数链接到HTTP请求主体.使用HttpMessageConverters将参数值转换为声明的方法参数类型.此批注指示应将方法参数绑定到Web请求的主体.

例如,Spring RequestBody的Angular请求看起来像这样:

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

RequestBody的端点:

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

希望这可以帮助.


山茶树*_*葡萄树 10

映射 HTTP 请求头Content-Type,处理请求正文。

  • @RequestParam? application/x-www-form-urlencoded,

  • @RequestBody? application/json,

  • @RequestPart? multipart/form-data,



Dul*_*sta 6

@RequestParam 使Spring将请求参数从GET / POST请求映射到您的方法参数。

GET请求

http://testwebaddress.com/getInformation.do?city=Sydney&country=Australia

public String getCountryFactors(@RequestParam(value = "city") String city, 
                    @RequestParam(value = "country") String country){ }
Run Code Online (Sandbox Code Playgroud)

POST请求

@RequestBody使Spring将整个请求映射到模型类,然后您可以从那里获取其getter和setter方法的值或为其设置值。检查下面。

http://testwebaddress.com/getInformation.do
Run Code Online (Sandbox Code Playgroud)

您有这样的JSON数据来自前端,并击中了控制器类

{
   "city": "Sydney",
   "country": "Australia"
}
Run Code Online (Sandbox Code Playgroud)

Java代码-后端(@RequestBody

public String getCountryFactors(@RequestBody Country countryFacts)
    {
        countryFacts.getCity();
        countryFacts.getCountry();
    }


public class Country {

    private String city;
    private String country;

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }
}
Run Code Online (Sandbox Code Playgroud)


Raf*_* G. 5

@RequestParam注解告诉Spring它应该将GET / POST请求中的请求参数映射到您的方法参数。例如:

请求:

GET: http://someserver.org/path?name=John&surname=Smith
Run Code Online (Sandbox Code Playgroud)

端点代码:

public User getUser(@RequestParam(value = "name") String name, 
                    @RequestParam(value = "surname") String surname){ 
    ...  
    }
Run Code Online (Sandbox Code Playgroud)

因此,基本上,虽然@RequestBody将整个用户请求(甚至用于POST)映射到String变量,@RequestParam但使用一个(或多个-但更复杂的)请求参数映射到方法参数。

  • 请举例说明。@ RequestBody (4认同)

小智 5

这很简单,只需查看它们的名称@RequestParam,它由两部分组成,一部分是“Request”,这意味着它将处理请求,另一部分是“Param”,这本身就有意义,它将仅映射对 java 对象的请求。@RequestBody 的情况也是如此,它将处理通过请求到达的数据,就像客户端发送 json 对象或带有请求的 xml 一样,此时必须使用 @requestbody。