常用HTTP contentType与后端处理方式
1、Content-Type:application/x-www-form-urlencoded; charset=UTF-8
![](https://upload-images.jianshu.io/upload_images/6487390-ef0659504ed4c934.png?imageMogr2/auto-orient/strip|imageView2/2/w/1200/format/webp)
![](https://upload-images.jianshu.io/upload_images/6487390-a7558772cda69b2d.png?imageMogr2/auto-orient/strip|imageView2/2/w/1014/format/webp)
对应的后端的处理方式,如果是单个参数去获取,那就用@RequestParam,如果是对应Java Bean,那就不需要任何注解
2、Content-Type:application/json;charset=UTF-8
![](https://upload-images.jianshu.io/upload_images/6487390-e95b480d6e8e9fd8.png?imageMogr2/auto-orient/strip|imageView2/2/w/1200/format/webp)
对应的后端处理方式,设置@RequestBody
![](https://upload-images.jianshu.io/upload_images/6487390-b594319c2bf2b8c8.png?imageMogr2/auto-orient/strip|imageView2/2/w/1058/format/webp)
3、Content-Type:multipart/form-data;
![](https://upload-images.jianshu.io/upload_images/6487390-7ee71adf705e07f1.png?imageMogr2/auto-orient/strip|imageView2/2/w/1200/format/webp)
![](https://upload-images.jianshu.io/upload_images/6487390-0680437c6c79172a.png?imageMogr2/auto-orient/strip|imageView2/2/w/1070/format/webp)
2018-1-29下班总结:
用get方式发送参数,拼接在url后:
![](https://upload-images.jianshu.io/upload_images/6487390-ef471f11647397b8.png?imageMogr2/auto-orient/strip|imageView2/2/w/726/format/webp)
后端处理:
![](https://upload-images.jianshu.io/upload_images/6487390-763f506d3d14b368.png?imageMogr2/auto-orient/strip|imageView2/2/w/575/format/webp)
![](https://upload-images.jianshu.io/upload_images/6487390-cbb90f62fd13b281.png?imageMogr2/auto-orient/strip|imageView2/2/w/508/format/webp)
如果是Post请求:
1、前端传递的方式为:Body+application/x-www-form-urlencoded:
![](https://upload-images.jianshu.io/upload_images/6487390-d3ceea7620aa194f.png?imageMogr2/auto-orient/strip|imageView2/2/w/852/format/webp)
![](https://upload-images.jianshu.io/upload_images/6487390-31f464bebcb6da05.png?imageMogr2/auto-orient/strip|imageView2/2/w/1200/format/webp)
2、如果前端改成以json字符串的形式传递参数:
![](https://upload-images.jianshu.io/upload_images/6487390-783c046c250ebc5c.png?imageMogr2/auto-orient/strip|imageView2/2/w/1152/format/webp)
后端接收到json字符串,用阿里fastjson解析:
![](https://upload-images.jianshu.io/upload_images/6487390-8a439366cc41f7ce.png?imageMogr2/auto-orient/strip|imageView2/2/w/581/format/webp)
3、接着上一步的后端代码,如果前端改成form-data 的方式传递,则报错:
![](https://upload-images.jianshu.io/upload_images/6487390-6136de800de0ecdf.png?imageMogr2/auto-orient/strip|imageView2/2/w/1200/format/webp)
将后端的代码改一下:
![](https://upload-images.jianshu.io/upload_images/6487390-9d09fe7c568a4e32.png?imageMogr2/auto-orient/strip|imageView2/2/w/1034/format/webp)
2、2如果前端是body+x-www-form-urlencoded方式传参,后端在解析字符串时报错:
![](https://upload-images.jianshu.io/upload_images/6487390-5b6c6bab2d15f697.png?imageMogr2/auto-orient/strip|imageView2/2/w/956/format/webp)
到这里,我们小小总结一下:
如果是POST+form-data的方式:
不需要做任何处理,直接用对象接收即可(不管有多少个对象):
![](https://upload-images.jianshu.io/upload_images/6487390-3f31481f796ad9df.png?imageMogr2/auto-orient/strip|imageView2/2/w/480/format/webp)
如果是POST+x-www.form-urlencoded的方式,可以加@RequestParam注解来获取(如果不传,则报错:
![](https://upload-images.jianshu.io/upload_images/6487390-dd4a340a920bc2f5.png?imageMogr2/auto-orient/strip|imageView2/2/w/856/format/webp)
),也可以不加@RequestParam来获取,获取不到值的时候不报错。
如果是POSt+json格式传递(浏览器为Request Payload,postman为raw格式)的方式:
![](https://upload-images.jianshu.io/upload_images/6487390-abc5d2d162839883.png?imageMogr2/auto-orient/strip|imageView2/2/w/1142/format/webp)
后端需要用@RequestBody注解标识字段,@RequestBody的作用是接收Http请求中参数,调用HttpMessageConverter接口转化为json或xml数据并绑定到对象上。同理,@ResponseBody也是调用HttpMessageConverter接口将数据转换成json或xml格式的数据,返回给前端。
![](https://upload-images.jianshu.io/upload_images/6487390-a7ff94f35f1d21d8.png?imageMogr2/auto-orient/strip|imageView2/2/w/996/format/webp)
最终的结论:
json格式的数据用@RequestBody注解让HttpMessageConverter转换成json数据绑定到对象上;
urlencoded或者form-data一般都用@RequestParam注解来标注(算是个规范吧,不加也可以),当是自己封装的实体对象时,什么都不用加。