常用HTTP contentType与后端处理方式
1、Content-Type:application/x-www-form-urlencoded; charset=UTF-8


对应的后端的处理方式,如果是单个参数去获取,那就用@RequestParam,如果是对应Java Bean,那就不需要任何注解
2、Content-Type:application/json;charset=UTF-8

对应的后端处理方式,设置@RequestBody

3、Content-Type:multipart/form-data;


2018-1-29下班总结:
用get方式发送参数,拼接在url后:

后端处理:


如果是Post请求:
1、前端传递的方式为:Body+application/x-www-form-urlencoded:


2、如果前端改成以json字符串的形式传递参数:

后端接收到json字符串,用阿里fastjson解析:

3、接着上一步的后端代码,如果前端改成form-data 的方式传递,则报错:

将后端的代码改一下:

2、2如果前端是body+x-www-form-urlencoded方式传参,后端在解析字符串时报错:

到这里,我们小小总结一下:
如果是POST+form-data的方式:
不需要做任何处理,直接用对象接收即可(不管有多少个对象):

如果是POST+x-www.form-urlencoded的方式,可以加@RequestParam注解来获取(如果不传,则报错:

如果是POSt+json格式传递(浏览器为Request Payload,postman为raw格式)的方式:

后端需要用@RequestBody注解标识字段,@RequestBody的作用是接收Http请求中参数,调用HttpMessageConverter接口转化为json或xml数据并绑定到对象上。同理,@ResponseBody也是调用HttpMessageConverter接口将数据转换成json或xml格式的数据,返回给前端。

最终的结论:
json格式的数据用@RequestBody注解让HttpMessageConverter转换成json数据绑定到对象上;
urlencoded或者form-data一般都用@RequestParam注解来标注(算是个规范吧,不加也可以),当是自己封装的实体对象时,什么都不用加。