浏览器 报400 的 错误,在开发过程经常遇到,主要原因分为以下几种。
1.提交表单数据类型没有与实体保持一致。
2.方法参数顺序不正确
3.javabean的属性和你的form提交中的数据类型不匹配
4.对象类型与实体类型不一致
5.实体类型不能为private,应为protected、public
6.日期不匹配
7.前端传递的是json 字符串格式的,后台忘记加@RequestBody
下面主要说说ajax 中content-type 属性
1、当content-type 设置为 application/x-www-form-urlcoded (默认值)的时候,后台接受参数 不用加@RequestBody 注解,直接使用@Requestparam
接受或者什么都不用。
2、当content-type 设置为 application/json或者是application/xml 时,要用@RequestBody 来接受。这是传递的就是json 类型的字符串。
通过@RequestBody接受参数的具体例子。
注:每个方法中最多只能有一个@RequestBody。
1、@RequestBody 以String 的形式接受参数
public AjaxResult AcceptJsonParams(@RequestBody String json)
{
System.out.println(json);//{"name":"zhangsan","sex":"男"}
return null;
}
2、用实体类接受
Class User
{
public String name;
public String sex;
//省略set,get,toString方法
}
public AjaxResult AcceptJsonParams(@RequestBody User user)
{
System.out.println(user。toString());
//输出 User{name='zhangshan', sex='男'}
return null;
}
3、@RequestBody以复杂对象接收前端传过来的json数据
public AjaxResult AcceptJsonParams(@RequestBody User user)
{
System.out.println(user。toString());
//输出 User{name='zhangshan', sex='男', address=[Address{name='北京', code='277500'}, Address{name='上海', code='277501'}]}
return null;
}