spring 春天:@ModelAttribute VS @RequestBody
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21824012/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring: @ModelAttribute VS @RequestBody
提问by Touchstone
Please correct me if I am wrong. Both can be used for Data Binding.
如果我错了,请纠正我。两者都可以用于数据绑定。
The question is when to use @ModelAttribute?
问题是什么时候使用@ModelAttribute?
@RequestMapping(value="/owners/{ownerId}/pets/{petId}/edit", method = RequestMethod.POST)
public String processSubmit(@ModelAttribute Pet pet) { }
In addition, when to use @RequestBody?
另外,什么时候使用@RequestBody?
@RequestMapping(value = "/user/savecontact", method = RequestMethod.POST
public String saveContact(@RequestBody Contact contact){ }
According to my understanding both serves the similar purpose.
根据我的理解,两者都有类似的目的。
Thanks!!
谢谢!!
回答by CharlesC
The simplest way for my understanding is, the @ModelAttribute
will take a query string. so, all the data are being pass to the server through the url.
我理解的最简单的方法是,@ModelAttribute
将采用查询字符串。因此,所有数据都通过 url 传递到服务器。
As for @RequestBody
, all the data will be pass to the server through a full JSON body.
至于@RequestBody
,所有数据都将通过完整的 JSON 主体传递到服务器。
回答by Naveen raj
@ModelAttribute
is used for binding data from request param (in key value pairs),
@ModelAttribute
用于绑定来自请求参数的数据(在键值对中),
but @RequestBody
is used for binding data from whole body of the request like POST,PUT.. request types which contains other format like json, xml.
但@RequestBody
用于绑定来自整个请求主体的数据,如 POST、PUT.. 请求类型,其中包含其他格式,如 json、xml。
回答by kondrak
I find that @RequestBody (also annotating a class as @RestController) is better for AJAX requests where you have complete control over the contents of the request being issued and the contents are sent as either XML or JSON (because of Hymanson). This allows the contents to easily create a model object. Conversely, @ModelAttribute seems to be better suited to forms where there is a "command" object backing a form (which may not necessarily be a model object).
我发现@RequestBody(也将类注释为@RestController)更适合AJAX 请求,您可以完全控制发出的请求的内容,并将内容作为XML 或JSON 发送(因为Hymanson)。这允许内容轻松创建模型对象。相反,@ModelAttribute 似乎更适合有“命令”对象支持表单(不一定是模型对象)的表单。
回答by oko
You can directly access your "pet" object in view layer, if you use ModelAttributeannotation. Also, you can instantiate this object in a method on your controller to put your model. see this.
如果您使用ModelAttribute注释,您可以直接访问视图层中的“宠物”对象。此外,您可以在控制器上的方法中实例化此对象以放置模型。看到这个。
ModelAttributegives you a chance to use this object partial, but with RequestBody, you get all body of request.
ModelAttribute使您有机会使用这个对象部分,但使用RequestBody,您可以获得请求的所有主体。
回答by Rejuan
If you want to do file upload, you have to use @ModelAttribute
. With @RequestBody
, it's not possible. Sample code
如果要进行文件上传,则必须使用@ModelAttribute
. 用@RequestBody
,这是不可能的。示例代码
@RestController
@RequestMapping(ProductController.BASE_URL)
public class ProductController {
public static final String BASE_URL = "/api/v1/products";
private ProductService productService;
public ProductController(ProductService productService) {
this.productService = productService;
}
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public ProductDTO createProduct(@Valid @ModelAttribute ProductInput productInput) {
return productService.createProduct(productInput);
}
}
ProductInput class
产品输入类
@Data
public class ProductInput {
@NotEmpty(message = "Please provide a name")
@Size(min = 2, max = 250, message = "Product name should be minimum 2 character and maximum 250 character")
private String name;
@NotEmpty(message = "Please provide a product description")
@Size(min = 2, max = 5000, message = "Product description should be minimum 2 character and maximum 5000 character")
private String details;
@Min(value = 0, message = "Price should not be negative")
private float price;
@Size(min = 1, max = 10, message = "Product should have minimum 1 image and maximum 10 images")
private Set<MultipartFile> images;
}
回答by shubham bellale
I think @ModelAttribute and @RequestBody both are having same use, only difference is @ModelAttribute use for normal spring MVC and @RequestBody use for REST web service. It is @PathVariable and @PathParam. But in in both the cases we can mix it. we can use @PathVariable in REST and vice versa.
我认为@ModelAttribute 和@RequestBody 都有相同的用途,唯一的区别是@ModelAttribute 用于普通spring MVC 和@RequestBody 用于REST web 服务。它是@PathVariable 和@PathParam。但在这两种情况下,我们都可以混合使用。我们可以在 REST 中使用 @PathVariable,反之亦然。