@RequestBody 和@RequestParam 有什么区别?
What is difference between @RequestBody and @RequestParam?
我已经通过Spring文档了解@RequestBody
,他们给出了以下解释:
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 the DefaultAnnotationHandlerMapping
and AnnotationMethodHandlerAdapter
. In Spring 3.0 the AnnotationMethodHandlerAdapter
is extended to support the @RequestBody
and has the following HttpMessageConverter
s registered by default:
...
但我的困惑是他们在文档中写的句子
The @RequestBody method parameter annotation indicates that a method parameter should be bound to the value of the HTTP request body.
这是什么意思?谁能给我举个例子吗?
spring 文档中的 @RequestParam
定义是
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.
我已经把他们弄糊涂了。请帮我举例说明它们之间的区别。
@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
将一个(或多个 - 但它更复杂) ) 向您的方法参数请求参数。
@RequestParam
带注释的参数链接到特定的 Servlet 请求参数。参数值被转换为声明的方法参数类型。
这个注解表示一个方法参数应该绑定到一个web请求参数上。
例如 Angular 请求 Spring RequestParam(s) 看起来像这样:
$http.post('http://localhost:7777/scan/l/register?username="Johny"&password="123123"&auth=true')
.success(function (data, status, headers, config) {
...
})
请求参数端点:
@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 请求的主体。
例如 Angular 请求 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为例,先看controller!!
public ResponseEntity<Void> postNewProductDto(@RequestBody NewProductDto newProductDto) {
...
productService.registerProductDto(newProductDto);
return new ResponseEntity<>(HttpStatus.CREATED);
....
}
这里是 angular 控制器
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>
@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){ }
POST 请求
@RequestBody
使 Spring 将整个请求映射到模型 class,然后您可以从那里检索或设置其 getter 和 setter 的值方法。检查下面。
http://testwebaddress.com/getInformation.do
你有 JSON
数据来自前端并到达你的控制器 class
{
"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;
}
}
映射 HTTP 请求头 Content-Type
,处理请求正文。
@RequestParam
← application/x-www-form-urlencoded
,
@RequestBody
← application/json
,
@RequestPart
← multipart/form-data
,
RequestParam (Spring Framework 5.1.9.RELEASE API)
map to query parameters, form data, and parts in multipart requests.
RequestParam
is likely to be used with name-value form fields
RequestBody (Spring Framework 5.1.9.RELEASE API)
bound to the body of the web request. The body of the request is passed through an HttpMessageConverter to resolve the method argument depending on the content type
of the request. (e.g. JSON, XML)
RequestPart (Spring Framework 5.1.9.RELEASE API)
used to associate the part of a "multipart/form-data
" request
RequestPart
is likely to be used with parts containing more complex content
HttpMessageConverter (Spring Framework 5.1.9.RELEASE API)
a converter that can convert from and to HTTP requests and responses.
All Known Implementing Classes: ..., AbstractJsonHttpMessageConverter, AbstractXmlHttpMessageConverter, ...
很简单,看他们的名字@RequestParam,它由两部分组成,一部分是"Request",这意味着它将处理请求,另一部分是"Param",这本身就有意义它只会将请求的参数映射到 java 个对象。
与@RequestBody 的情况相同,它将处理随请求到达的数据,例如如果客户端已发送 json 对象或 xml 时必须使用请求。
我已经通过Spring文档了解@RequestBody
,他们给出了以下解释:
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:...
但我的困惑是他们在文档中写的句子
The @RequestBody method parameter annotation indicates that a method parameter should be bound to the value of the HTTP request body.
这是什么意思?谁能给我举个例子吗?
spring 文档中的 @RequestParam
定义是
Annotation which indicates that a method parameter should be bound to a web request parameter. Supported for annotated handler methods in
Servlet
andPortlet
environments.
我已经把他们弄糊涂了。请帮我举例说明它们之间的区别。
@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
将一个(或多个 - 但它更复杂) ) 向您的方法参数请求参数。
@RequestParam
带注释的参数链接到特定的 Servlet 请求参数。参数值被转换为声明的方法参数类型。
这个注解表示一个方法参数应该绑定到一个web请求参数上。
例如 Angular 请求 Spring RequestParam(s) 看起来像这样:
$http.post('http://localhost:7777/scan/l/register?username="Johny"&password="123123"&auth=true')
.success(function (data, status, headers, config) {
...
})
请求参数端点:
@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 请求的主体。
例如 Angular 请求 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为例,先看controller!!
public ResponseEntity<Void> postNewProductDto(@RequestBody NewProductDto newProductDto) {
...
productService.registerProductDto(newProductDto);
return new ResponseEntity<>(HttpStatus.CREATED);
....
}
这里是 angular 控制器
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>
@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){ }
POST 请求
@RequestBody
使 Spring 将整个请求映射到模型 class,然后您可以从那里检索或设置其 getter 和 setter 的值方法。检查下面。
http://testwebaddress.com/getInformation.do
你有 JSON
数据来自前端并到达你的控制器 class
{
"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;
}
}
映射 HTTP 请求头 Content-Type
,处理请求正文。
@RequestParam
←application/x-www-form-urlencoded
,@RequestBody
←application/json
,@RequestPart
←multipart/form-data
,
RequestParam (Spring Framework 5.1.9.RELEASE API)
map to query parameters, form data, and parts in multipart requests.
RequestParam
is likely to be used with name-value form fieldsRequestBody (Spring Framework 5.1.9.RELEASE API)
bound to the body of the web request. The body of the request is passed through an HttpMessageConverter to resolve the method argument depending on the
content type
of the request. (e.g. JSON, XML)RequestPart (Spring Framework 5.1.9.RELEASE API)
used to associate the part of a "
multipart/form-data
" requestRequestPart
is likely to be used with parts containing more complex contentHttpMessageConverter (Spring Framework 5.1.9.RELEASE API)
a converter that can convert from and to HTTP requests and responses.
All Known Implementing Classes: ..., AbstractJsonHttpMessageConverter, AbstractXmlHttpMessageConverter, ...
很简单,看他们的名字@RequestParam,它由两部分组成,一部分是"Request",这意味着它将处理请求,另一部分是"Param",这本身就有意义它只会将请求的参数映射到 java 个对象。 与@RequestBody 的情况相同,它将处理随请求到达的数据,例如如果客户端已发送 json 对象或 xml 时必须使用请求。