1、@RequestBody注解,内部使用RequestResponseBodyMethodProcessor进行解析,此参数解析器用HttpMessageConverter将HttpMessage封装为具体的JavaBean对象。
@RequestBody 一般用于post请求,处理body体中的数据,headers中 Content-Type 设置为 application/json或 application/xml,后端根据前端传输的数据结构使用自定义的实体类或者map接收。
var request = require("request");
var options = { method: 'POST',
url: 'http://10.0.13.63:8086/my_prop/getInfo2',
headers:{'Content-Type': 'application/json' },
body: { code: 'no1001', name: 'lily' },
json: true };
request(options, function (error, response, body) {
if (error) throw new Error(error);
console.log(body);
});
------------------------后端----------------------------
@SuppressWarnings("serial")
@PostMapping("getInfo2")
public Object getInfo2(@RequestBody Map<String,String> map) {
return new HashMap<String,String>(){{
put("new_name","lily");
put("new_code","no1001");
put("name",map.get("name"));
put("code",map.get("code"));
}};
}
-----------------------返回-----------------------------
{
"code": "no1001",
"name": "lily",
"new_code": "no1002",
"new_name": "lily2"
}
get请求也可以使用,参数放在body中,headers中 Content-Type 同样设置为 application/json 或 application/xml,
PS:之前一直认为get请求参数不能放body中,实验了下才知道可以,设置好请求头,使用@RequestBody 就可以接收到
var request = require("request");
var options = { method: 'GET',
url: 'http://10.0.13.63:8086/my_prop/getInfo2',
headers:
{ 'Content-Type': 'application/json' },
body: { code: 'no1001', name: 'lily' },
json: true };
request(options, function (error, response, body) {
if (error) throw new Error(error);
console.log(body);
});
------------java--------------
@SuppressWarnings("serial")
@GetMapping("getInfo2")
public Object getInfo2(@RequestBody Map<String,String> map) {
return new HashMap<String,String>(){{
put("new_name","lily2");
put("new_code","no1002");
put("name",map.get("name"));
put("code",map.get("code"));
}};
}
--------------返回--------------
{
"code": "no1001",
"name": "lily",
"new_code": "no1002",
"new_name": "lily2"
}
如果 Content-Type 是 application/x-www-form-urlencoded ,其实@RequestBody也是能接收到数据的,只是此时@RequestBody需要将body中的所有数据都作为一个字符串接收
var request = require("request");
var options = { method: 'GET',
url: 'http://10.0.13.63:8086/my-prop/getInfo3',
headers:
{'Content-Type': 'application/x-www-form-urlencoded' },
form: { name: 'jack', code: 'no001', undefined: undefined } };
request(options, function (error, response, body) {
if (error) throw new Error(error);
console.log(body);
});
---------------------------------------------
@SuppressWarnings("serial")
@GetMapping("getInfo3")
public Object getInfo3(@RequestBody String name) {
return new HashMap<String,String>(){{
put("new_name","lily2");
put("new_code","no1002");
put("name",name);
}};
}
-------------------------------------------
{
"name": "name=jack&code=no001",
"new_code": "no1002",
"new_name": "lily2"
}
如果前端传的是一个复杂对象,后端不方便使用一个简单实体类接收,又不想再创建一个复杂实体类的时候,可以使用Map<String,Object>接收,在转化传为对应的实体类。
2、@RequestParam 一般用于get请求, 处理params 中的数据,即URL后面的xxx=xxxx&yyyy=yyyy, 使用多个@RequestParam String xxx 接收,或者Map<String,String> map全部接收。
@RequestParam也可处理post请求中body中的数据,只要将Content-Type 设置为 application/x-www-form-urlencoded 即可。
注:@RequestBody 注解一个方法中只能使用一个, @RequestParam注解一个方法中可以存在多个, @RequestBody 可以和@RequestParam同时存在于一个方法中,一个处理body中的数据,一个处理params 中的数据,一般不建议这样做。
注:前端使用params 或者 body application/x-www-form-urlencoded 方式传值,后端如果想用自定义的实体类接收,去掉@RequestParam注解即可,spring 会自动处理。
@SuppressWarnings("serial")
@PostMapping("getInfo4")
public Object getInfo4(SsoUser ssoUser) {
return new HashMap<String,String>(){{
put("new_name","lily");
put("new_code","no1001");
put("code",ssoUser.getPassword());
put("name",ssoUser.getAccount());
}};
}