赞
踩
1、 @RequestBody主要用来接收前端传递给后端的json字符串中的数据的(请求体中的数据的);
2、GET方式无请求体,所以使用@RequestBody接收数据时,前端不能使用GET方式提交数据,而是用POST方式进行提交;
3、在后端的同一个接收方法里,@RequestBody与@RequestParam()可以同时使用,@RequestBody最多只能有一个,而@RequestParam()可以有多个;
1、如果参数时放在请求体中,传入后台的话,那么后台要用@RequestBody才能接收到;如果不是放在 请求体中的话,那么后台接收前台传过来的参数时,要用@RequestParam来接收;
2、如果参数前写了@RequestParam(xxx),那么前端必须有对应的xxx名字才行(不管其是否有值,当然可以通过设置该注解的required属性来调节是否必须传),如果没有xxx名的话,那么请求会出错,报400;
3、如果后端参数是一个对象,且该参数前是以@RequestBody修饰的,那么前端传递json参数时,必须满足以下要求:
a:后端@RequestBody注解对应的类在将HTTP的输入流(含请求体)装配到目标类时,会根据json字符串中的key来匹配对应实体类的属性;
b:json字符串中,如果value为""的话,后端对应属性如果是String类型的,那么接受到的就是"",如果是后端属性的类型是Integer、Double等类型,那么接收到的就是null;
c:json字符串中,如果value为null的话,后端对应收到的就是null;
d:如果某个参数没有value的话,在传json字符串给后端时,要么干脆就不把该字段写到json字符串中;要么写value时, 必须有值,null 或""都行。千万不能有类似"stature":
@RequestMapping("mytest0")
public String test0(@RequestBody String jsonString){
System.out.println(jsonString);
return jsonString;
}
@RequestMapping("mytest1") public String test1(@RequestBody User user){ System.out.println(user.toString()); return user.toString(); }
<span class="token keyword">public</span> Class <span class="token class-name">User</span><span class="token punctuation">{<!-- --></span> <span class="token keyword">private</span> String name<span class="token punctuation">;</span> <span class="token keyword">private</span> Integer age<span class="token punctuation">;</span> <span class="token keyword">private</span> String gender<span class="token punctuation">;</span> <span class="token keyword">private</span> String motto<span class="token punctuation">;</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>get<span class="token operator">/</span>set方法 <span class="token annotation punctuation">@Override</span> <span class="token keyword">public</span> String <span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span> <span class="token keyword">return</span> age <span class="token operator">+</span> <span class="token string">"岁"</span> <span class="token operator">+</span> gender <span class="token operator">+</span> <span class="token string">"人【"</span> <span class="token operator">+</span> name <span class="token operator">+</span> <span class="token string">"]的座右铭居然是:"</span> <span class="token operator">+</span> motto <span class="token operator">+</span> <span class="token string">"!!!"</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span>
@RequestMapping("mytest2")
public String test2(@RequestBody Team team){
System.out.println(team.toString());
return team.toString();
}
public class team{
private Integer id;
private String teamName;
private List<String> honors;
private List<User> teamMembers;
...get/set方法
...toString方法
}
@RequestMapping("mytest3")
public String test3(@RequestBody User user, @RequestParam("token") String token){
return token + ">>>" + user.toString();
}
@RequestMapping("mytest5")
public String test5(@RequestBody User user, @RequestParam("arrays") List<String> arrays){
StringBuffer sb = new StringBuffer();
for (String array : arrays){
sb.append(array);
sb.append(" ");
}
return sb.toString() + ser.toString();
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。