最近使用http请求测试接口,传参数格式给了自己狠狠大的困惑今天花时间总结下。
当你的controller使用post接收请求的时候 传参一般是 封装类 和封装的对象 需要注解@RequestBody @RequestParam 才能接收到json的参数 不然是接收不到的比如
两个接口
@ApiOperation(value = "登录")
@RequestMapping(value = "/login",method = RequestMethod.POST)
@ResponseBody
public ViewResult login(@RequestBody UserRequest userRequest) {
return userService.login(userRequest);
}
@ApiOperation(value = "登录")
@RequestMapping(value = "/login",method = RequestMethod.POST)
@ResponseBody
public ViewResult login(UserRequest userRequest) {
return userService.login(userRequest);
}
http请求 当你的idea使用http cliect发请求时
第一个接口接收到参数 第二个可以接收不到参数
###
POST http://localhost:6688/gzl-oauth/oauth/login
Content-Type: application/json
Authorization:
{
"account": "gzlwork",
"password": "gzlwork"
}
第一个接口接收不到参数 第二个可以接收到参数
###
POST http://localhost:6688/gzl-oauth/oauth/login
Content-Type: application/x-www-form-urlencoded
account=gzlwork&password=gzlwork
###
POST http://localhost:6688/gzl-oauth/oauth/login?account=gzlwork&password=gzlwork
Content-Type: application/x-www-form-urlencoded
这两种发送参数的方式没有区别
当你传参数时 封装类单个参数的时候
@ApiOperation(value = "登录")
@RequestMapping(value = "/login",method = RequestMethod.POST)
@ResponseBody
public ViewResult login(@RequestParam(name) String name) {
return userService.login(name);
}
@ApiOperation(value = "登录")
@RequestMapping(value = "/login",method = RequestMethod.POST)
@ResponseBody
public ViewResult login(String name) {
return userService.login(name);
}
接口都可以接收到参数的,
###
POST http://localhost:6688/gzl-oauth/oauth/loginForm
Content-Type: application/x-www-form-urlencoded
Authorization: eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ7XCJhY2NvdW50XCI6XCJnemx3b3JrXCIsXCJhdmF0YXJcIjpcIiQyYSQxMCRuTlU5NUhwUlMzZHUuSWRxTFl3NjB1VVlFUlBkM1RicHhmM2k5ZTZTaWJpbFJ6SWh6ZmxMdVwiLFwiYmlydGhkYXlcIjpcIjIwMTgtMDgtMDhUMDA6MDA6MDBcIixcImNvZGVcIjpcIkQwMDAxXCIsXCJjcmVhdGVEZXB0XCI6MCxcImNyZWF0ZVRpbWVcIjpcIjIwMTgtMDgtMDhUMDA6MDA6MDBcIixcImNyZWF0ZVVzZXJcIjowLFwiZGVwdElkXCI6XCIxXCIsXCJlbWFpbFwiOlwiMTgzMzA4MDg4NDFAMTYzLmNvbVwiLFwiaWRcIjo0LFwiaXNEZWxldGVkXCI6MCxcIm5hbWVcIjpcIndyb2tcIixcInBhc3N3b3JkXCI6XCIkMmEkMTAkUnlqQnYxZEhBdEdNNUZnY25rZFVUTzlISmFLTEZ4RmZMb1RWL1FlcnZEdVd5dkhiZHFWOVdcIixcInBob25lXCI6XCIxODMzMDgwODg0MVwiLFwicG9zdElkXCI6XCIxXCIsXCJyZWFsTmFtZVwiOlwiaGVyb1wiLFwicm9sZUlkXCI6XCIxXCIsXCJzZXhcIjoxLFwic3RhdHVzXCI6MSxcInRlbmFudElkXCI6XCIwMDAwMDBcIixcInVwZGF0ZVRpbWVcIjpcIjIwMTgtMDgtMDhUMDA6MDA6MDBcIixcInVwZGF0ZVVzZXJcIjowfSIsIm5iZiI6MTcyNDQ5MDg2MDQyOCwiaXNzIjoiZ3psIiwiZXhwIjoxNzI0NTc3MjYwNDI4LCJpYXQiOjE3MjQ1NzcyNjAsImp0aSI6IjFmMDBlNmQ1ZjBkNDQyZGI4OTk0MTkxMWYyZWI5ZTAzIn0.CU8or9ClcO6phMyWcYRf8eRaUBh4m1_taPDNNFT6FHo
name=gzlwork
第一个接口接收到参数 第二个接口接收不到参数
###
POST http://localhost:6688/gzl-oauth/oauth/loginForm
Content-Type: application/json
Authorization: eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ7XCJhY2NvdW50XCI6XCJnemx3b3JrXCIsXCJhdmF0YXJcIjpcIiQyYSQxMCRuTlU5NUhwUlMzZHUuSWRxTFl3NjB1VVlFUlBkM1RicHhmM2k5ZTZTaWJpbFJ6SWh6ZmxMdVwiLFwiYmlydGhkYXlcIjpcIjIwMTgtMDgtMDhUMDA6MDA6MDBcIixcImNvZGVcIjpcIkQwMDAxXCIsXCJjcmVhdGVEZXB0XCI6MCxcImNyZWF0ZVRpbWVcIjpcIjIwMTgtMDgtMDhUMDA6MDA6MDBcIixcImNyZWF0ZVVzZXJcIjowLFwiZGVwdElkXCI6XCIxXCIsXCJlbWFpbFwiOlwiMTgzMzA4MDg4NDFAMTYzLmNvbVwiLFwiaWRcIjo0LFwiaXNEZWxldGVkXCI6MCxcIm5hbWVcIjpcIndyb2tcIixcInBhc3N3b3JkXCI6XCIkMmEkMTAkUnlqQnYxZEhBdEdNNUZnY25rZFVUTzlISmFLTEZ4RmZMb1RWL1FlcnZEdVd5dkhiZHFWOVdcIixcInBob25lXCI6XCIxODMzMDgwODg0MVwiLFwicG9zdElkXCI6XCIxXCIsXCJyZWFsTmFtZVwiOlwiaGVyb1wiLFwicm9sZUlkXCI6XCIxXCIsXCJzZXhcIjoxLFwic3RhdHVzXCI6MSxcInRlbmFudElkXCI6XCIwMDAwMDBcIixcInVwZGF0ZVRpbWVcIjpcIjIwMTgtMDgtMDhUMDA6MDA6MDBcIixcInVwZGF0ZVVzZXJcIjowfSIsIm5iZiI6MTcyNDQ5MDg2MDQyOCwiaXNzIjoiZ3psIiwiZXhwIjoxNzI0NTc3MjYwNDI4LCJpYXQiOjE3MjQ1NzcyNjAsImp0aSI6IjFmMDBlNmQ1ZjBkNDQyZGI4OTk0MTkxMWYyZWI5ZTAzIn0.CU8or9ClcO6phMyWcYRf8eRaUBh4m1_taPDNNFT6FHo
{
"account":"gzlwork"
}
Content-Type: application/json 模式接口实现时需要添加@RequestBody @RequestParam 注解 接口才能接收到http传的json数据
如果没有添加注解 默认的是Content-Type: application/x-www-form-urlencoded 表单提交 我们也能注意到 当使用@RequestParam注解的时候,使用表单提交参数也是能接收到参数的