@RequestBody与@RequestParam区别
在了解两者区别前要明白http求情报文的组成
http请求 = 请求行 + 请求头 + 请求体
① 请求方法, GET和POST是最常见的HTTP方法 ,除此之外还有DELETE,--- PUT 用于RESTFUL
② 请求对应的url地址, 它和报文头的Host属性组成完整的请求url
③ 协议及版本号
④ http的报文头, 报文头包含若干个属性, 格式为 " 属性名: 属性值 " ,服务端根据此获取客户端(浏览器)的信息
-
Accept:text/plain response 接受类型
-
Cache-Control: no-cache
-
Accept-Charset:utf-8
-
Authorization:
-
Content-Type: request体类型
-
host: 域名
-
refer: 转发
get请求的方式没有Content-Type, 只有Post的请求由Content-Type ,通过表单的enctype属性来修改
其中常见的 Content-Type有:
① application/x-www-form-urlencoded
-
原生的form表单, 默认采用该Content-Type, 所有字符都会进行编码
POST http://192.168.2.12/index HTTP/1.1 Content-Type: application/x-www-form-urlencoded;charset=utf-8 title=test&sub%5B%5D=1&sub%5B%5D=2&sub%5B%5D=3
提交的数据按照 key1=val1&key2=val2 的方式进行编码,key 和 val 都进行了 URL 转码
-
Spring
中用@RequestParam来处理Content-Type
为application/x-www-form-urlencoded
数据。
修饰的对象可以是基本数据类型和自定义对象。
②multipart/form-data
使用表单上传文件时,必须将 的 enctype
设为 multipart/form-data
POST http://192.168.2.12/index HTTP/1.1 Content-Type:multipart/form-data; boundary=--WebKitFormBoundaryrGKCBY7qhFd3TrwA ---WebKitFormBoundaryrGKCBY7qhFd3TrwA Content-Disposition: form-data; name="text" title ---WebKitFormBoundaryrGKCBY7qhFd3TrwA Content-Disposition:form-data;name="file"; filename="chrome.png" Content-Type: image/png PNG ... content of chrome.png ... ---WebKitFormBoundaryrGKCBY7qhFd3TrwA
-
spring
使用@RequestPart处理接收到的文件@RequestMapping("uploadFile") public JsonResult uploadFile(@RequestPart("file") MultipartFile file, @RequestParam String bucket){ String fileUrl = aliossService.uploadFile(file, bucket); Map<String,String> result = new HashMap<>(); result.put("fileUrl",fileUrl); return success(result); }
③ application/json
-
application/json
作为请求头,用来告诉服务端消息主体是序列化的JSON字符串例如ajax将json串传到后端
POST http://www.example.com HTTP/1.1 Content-Type: text/xml <?xml version="1.0"?> <methodCall> <methodName>examples.getStateName</methodName> <params> <param> <value><i4>41</i4></value> </param> </params> </methodCall>
-
spring
中通过@RequstBody来接收
④ text/xml
典型的 XML-RPC 请求是这样的:
POST http://www.example.com HTTP/1.1 Content-Type: text/xml <?xml version="1.0"?> <methodCall> <methodName>examples.getStateName</methodName> <params> <param> <value><i4>41</i4></value> </param> </params> </methodCall>
参考转载自