常用Rest API各类注解参数接收对照

Spring boot

1、@RequestParam

可接收QueryString参数或 Content-Type: mutilpart/form-data的表单参数
或 Content-Type: application/x-www-form-urlencoded的表单参数(需要对象类型)

//接收单个变量,默认必选
func(@RequestParam String testKey){
}
//接收对象,获取所有参数(包括QueryString和x-www-form-urlencoded表单)
func(@RequestParam Map testKey){
}
地址 方法 Content-Type 结果 原因
1. http://location:8080/?testKey=1 POST和GET all 所有 成功
2. http://location:8080/ POST form-data 成功
3. http://location:8080/ POST x-www-form-urlencoded 成功
4. http://location:8080/ POST application/json 失败 @RequestBody接收为对象

2、@RequestBody

可接收Content-Type: application/json参数

//
func(@RequestBody Object testKey){
}
//
func(@RequestParam Map testKey){
}
地址 方法 Content-Type 结果 原因
1. http://location:8080/?testKey=1 POST和GET all 所有 失败
2. http://location:8080/ POST form-data 失败 不支持
3. http://location:8080/ POST x-www-form-urlencoded 失败 不支持
4. http://location:8080/ POST application/json 成功

3、@RequestHeader

可接收Header的参数

//
func(@RequestHeader String testKey){
}

4、@PathVariable

接口路由声明的变量参数

//
@RequestMapper(value = "/user/{user_id}/{order_id}")
func(@PathVariable(value = "user_id") String userId){
}

Nodejs

Nodejs的处理方式有点不太一样,主要看安装的解释库的处理。

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());

1、req.param(req.params)

地址 方法 Content-Type 结果 原因
1. http://location:8080/?testKey=1 POST和GET all 所有 失败
2. http://location:8080/ POST form-data 失败
3. http://location:8080/ POST x-www-form-urlencoded 失败
4. http://location:8080/ POST application/json 失败

2、req.query

地址 方法 Content-Type 结果 原因
1. http://location:8080/?testKey=1 POST和GET all 所有 成功
2. http://location:8080/ POST form-data 失败
3. http://location:8080/ POST x-www-form-urlencoded 失败
4. http://location:8080/ POST application/json 失败

3、req.body

地址 方法 Content-Type 结果 原因
1. http://location:8080/?testKey=1 POST和GET all 所有 失败
2. http://location:8080/ POST form-data 失败 需要库另行解释
3. http://location:8080/ POST x-www-form-urlencoded 成功
4. http://location:8080/ POST application/json 成功

dotnet(c# owin)

1、[FromUri]

地址 方法 Content-Type 结果 原因
1. http://location:8080/?testKey=1 POST和GET all 所有 成功
2. http://location:8080/ POST form-data 失败
3. http://location:8080/ POST x-www-form-urlencoded 失败
4. http://location:8080/ POST application/json 失败

1、[FromBody]

地址 方法 Content-Type 结果 原因
1. http://location:8080/?testKey=1 POST和GET all 所有 失败
2. http://location:8080/ POST form-data 失败 需要另行处理
3. http://location:8080/ POST x-www-form-urlencoded 成功
4. http://location:8080/ POST application/json 成功

你可能感兴趣的:(常用Rest API各类注解参数接收对照)