spring中@RequestParam和@RequestBody的区别

@RequestBody @RequestParam
可以处理少部分Content-Type不是application/x-www-form-urlencoded编码格式的数据(如json xml格式等),但是有很多限制 用来处理使用Content-Type 为 application/x-www-form-urlencoded等格式编码的数据(大多数情况用这个)
不可以接收 multipart/form-data 类型的请求 可以接收 multipart/form-data 类型的请求(即文件上传请求)
get请求不可以使用,post请求才可以使用 get、post请求都可以使用

注意:
当前台不指定Content-Type的时候,默认使用application/x-www-form-urlencoded格式传输。

总结:

从个人习惯来说,一般情况下,当前台使用非json,xml格式向后台发数据的时候,都使用@RequestParam就好了;当使用json,xml格式传输的时候,再去使用@RequestBody,这样可以防止被诸多@RequestBody的限制干扰,导致程序报错。

猜你喜欢

转载自blog.csdn.net/qq_37856300/article/details/86562481