96

我已经浏览了 Spring 文档来了解@RequestBody,他们给出了以下解释:

@RequestBody方法参数注解表示方法参数应该绑定到 HTTP 请求体的值。例如:

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

您可以使用HttpMessageConverter. HttpMessageConverter负责将 HTTP 请求消息转换为对象,并将对象转换为 HTTP 响应体。

DispatcherServletDefaultAnnotationHandlerMapping支持使用和进行基于注释的处理AnnotationMethodHandlerAdapter。在 Spring 3.0 中,AnnotationMethodHandlerAdapter扩展为支持并且默认注册@RequestBody了以下s:HttpMessageConverter

...

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

@RequestBody 方法参数注解表示方法参数应该绑定到 HTTP 请求体的值。

他们是什么意思?谁能给我一个例子?

spring doc中的@RequestParam定义是

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

我在他们之间变得很困惑。请帮我举一个例子,说明它们之间的区别。

4

6 回答 6

114

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

例如,对 Spring RequestParam(s) 的 Angular 请求如下所示:

$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 请求的正文。

例如,对 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) {
                            ...
                        })

带有 RequestBody 的端点:

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

希望这可以帮助。

于 2015-10-06T21:54:55.633 回答
18

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

  • @RequestParamapplication/x-www-form-urlencoded,

  • @RequestBodyapplication/json,

  • @RequestPartmultipart/form-data,


于 2019-08-03T15:24:54.353 回答
12

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

获取请求

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

public String getCountryFactors(@RequestParam(value = "city") String city, 
                    @RequestParam(value = "country") String country){ }

POST 请求

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

http://testwebaddress.com/getInformation.do

您有JSON来自前端的数据并命中您的控制器类

{
   "city": "Sydney",
   "country": "Australia"
}

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;
    }
}
于 2018-05-17T08:48:56.423 回答
7

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

要求:

GET: http://someserver.org/path?name=John&surname=Smith

端点代码:

public User getUser(@RequestParam(value = "name") String name, 
                    @RequestParam(value = "surname") String surname){ 
    ...  
    }

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

于 2015-01-20T07:33:08.970 回答
4

这很简单,只要看看他们的名字@RequestParam 它由两部分组成,一个是“Request”,这意味着它将处理请求,另一部分是“Param”,这本身很有意义,它只会映射对 java 对象的请求。@RequestBody 的情况也是如此,它将处理随请求到达的数据,例如客户端是否发送了带有请求的 json 对象或 xml,此时必须使用 @requestbody。

于 2019-08-12T07:03:05.047 回答
1

这是@RequestBody 的示例,首先看控制器!

  public ResponseEntity<Void> postNewProductDto(@RequestBody NewProductDto newProductDto) {

   ...
        productService.registerProductDto(newProductDto);
        return new ResponseEntity<>(HttpStatus.CREATED);
   ....

}

这是角度控制器

function postNewProductDto() {
                var url = "/admin/products/newItem";
                $http.post(url, vm.newProductDto).then(function () {
                            //other things go here...
                            vm.newProductMessage = "Product successful registered";
                        }
                        ,
                        function (errResponse) {
                            //handling errors ....
                        }
                );
            }

简要介绍一下表格

 <label>Name: </label>
 <input ng-model="vm.newProductDto.name" />

<label>Price </label> 
 <input ng-model="vm.newProductDto.price"/>

 <label>Quantity </label>
  <input ng-model="vm.newProductDto.quantity"/>

 <label>Image </label>
 <input ng-model="vm.newProductDto.photo"/>

 <Button ng-click="vm.postNewProductDto()" >Insert Item</Button>

 <label > {{vm.newProductMessage}} </label>
于 2017-03-17T17:58:02.403 回答