名称匹配接收参数
入参和接收参数名称一致。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| @Controller @RequestMapping("/param") public class TestParamController { private static final Logger logger = LoggerFactory.getLogger(TestParamController.class);
@RequestMapping(value = "/add", method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"}) @ResponseBody public String addUser(String name, String pwd){ logger.debug("name:" + name + ",pwd:" + pwd); return "name:" + name + ",pwd:" + pwd; }
}
|
请求方式:
1
| curl http://localhost:8080/sty/param/add.action?name=张三&pwd=123456
|
对象属性名称匹配接收参数
Java实体类定义:
1 2 3 4 5
| @Data public class User { private String name; private String pwd; }
|
入参和接收对象的参数一致。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| @Controller @RequestMapping("/param") public class TestParamController { private static final Logger logger = LoggerFactory.getLogger(TestParamController.class);
@RequestMapping(value = "/addByObject", method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"}) @ResponseBody public String addUserByObject(User user){ logger.debug("name:" + user.getName() + ",pwd:" + user.getPwd()); return "name:" + user.getName() + ",pwd:" + user.getPwd(); } }
|
请求方式:
1
| curl http://localhost:8080/sty/param/addByObject.action?name=张三&pwd=123456
|
重命名接收参数
当请求参数名与方法参数名不一致时。
1 2 3 4 5 6 7 8 9 10 11 12
|
@RequestMapping(value = "/addByDifName", method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"}) @ResponseBody public String addUserByDifName(@RequestParam("name") String u_name, @RequestParam("pwd")String u_pwd){ logger.debug("name:" + u_name + ",pwd:" + u_pwd); return "name:" + u_name + ",pwd:" + u_pwd; }
|
请求方式:
1
| curl http://localhost:8080/sty/param/addUserByDifName.action?name=张三&pwd=123456
|
HttpServletRequest接收参数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
@RequestMapping(value = "/addByHttpServletRequest", method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"})
@ResponseBody
public String addUserByHttpServletRequest(HttpServletRequest request){
String name = request.getParameter("name");
String pwd = request.getParameter("pwd");
logger.debug("name:" + name + ",pwd:" + pwd);
return "name:" + name + ",pwd:" + pwd;
}
|
请求方式:
1
| curl http://localhost:8080/sty/param/addByHttpServletRequest.action?name=张三&pwd=123456
|
@PathVariable接收路径中的参数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
@RequestMapping(value = "/add/{name}/{pwd}", method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"})
@ResponseBody
public String addUserByPathVariable(@PathVariable String name, @PathVariable String pwd){
logger.debug("name:" + name + ",pwd:" + pwd);
return "name:" + name + ",pwd:" + pwd;
}
|
请求方式:
1
| curl http://localhost:8080/sty/param/add/zhangsan/123456.action
|
@RequestBody接收对象
接收JSON对象
需要依赖jackson-databind.jar,并设置json格式的支持。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
@RequestMapping(value = "/addByObjectJSON", produces = {"application/json;charset=UTF-8"})
@ResponseBody
public String addUserByObjectJSON(@RequestBody User user){
logger.debug("name:" + user.getName() + ",pwd:" + user.getPwd());
return "name:" + user.getName() + ",pwd:" + user.getPwd();
}
|
请求方式:
1 2 3 4 5 6 7
| curl --request POST \ --url http://localhost:8080/sty/param/addByObjectJSON.action \ --header 'content-type: application/json' \ --data '{ "name": "zhangsan", "pwd": "123456" }'
|
接收JSON数组
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
@RequestMapping(value = "/addByListJSON", produces = {"application/json;charset=UTF-8"}) @ResponseBody public String addUsersByListJSON(@RequestBody List<User> users){ StringBuilder sb = new StringBuilder("{"); if(null != users){ for(User user : users){ sb.append("{" + "name:" + user.getName() + ",pwd:" + user.getPwd() + "}"); } } sb.append("}"); logger.debug(sb.toString()); return sb.toString(); }
|
请求方式:
1 2 3 4 5 6 7 8 9 10 11 12 13
| curl --request POST \ --url http://localhost:8080/sty/param/addByObjectJSON.action \ --header 'content-type: application/json' \ --data '[ { "name": "zhangsan", "pwd": "123456" }, { "name": "lisi", "pwd": "654321" } ]'
|
接收Map对象
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
|
@RequestMapping(value = "/addByMapJSON", produces = {"application/json;charset=UTF-8"})
@ResponseBody
public String addUsersByMapJSON(@RequestBody Map<String, User> users){
StringBuilder sb = new StringBuilder("{");
if(null != users){
Iterator it = users.keySet().iterator();
while(it.hasNext()){
User user = users.get(it.next());
sb.append("{" + "name:" + user.getName() + ",pwd:" + user.getPwd() + "}");
}
}
sb.append("}");
logger.debug(sb.toString());
return sb.toString();
}
|
请求方式:
1 2 3 4 5 6 7 8 9 10 11 12 13
| curl --request POST \ --url http://localhost:8080/sty/param/addByObjectJSON.action \ --header 'content-type: application/json' \ --data '[ { "name": "zhangsan", "pwd": "123456" }, { "name": "lisi", "pwd": "654321" } ]'
|
(完)