以下事例使用ssm框架(项目名称为testProject)
事例一 @RequestBody
后端代码:
@RequestMapping(value = "/test")
@ResponseBody
public String test(@RequestBody Map<String, Object> params) {
Long startId = (Long) (params.get("startId"));
Integer totalSize = (Integer) (params.get("totalSize"));
System.out.println(startId + " " + totalSize);
return "success";
}
curl命令:
curl -i -X POST -H 'Content-Type:application/json' --data '{"startId":0L,"totalSize":10}' http://localhost:8080/testProject/test
事例二@RequestParam
后端代码:
@RequestMapping(value = "/test")
@ResponseBody
public String test(@RequestParam Map<String, String> params) {
Long startId = Long.parseLong (params.get("startId"));
Integer totalSize = Integer.parseInt (params.get("totalSize"));
System.out.println(startId + " " + totalSize);
return "success";
}
curl命令:
curl "http://localhost:8080/testProject/test?startId=0&totalSize=10"
事例三 不加注解
后端代码:
@RequestMapping(value = "/test")
@ResponseBody
public String test(Long startId, Integer totalSize, Integer threads) {
System.out.println(startId + " " + totalSize + " " + threads);
return "success";
}
curl命令:
curl "http://localhost:8080/testProject/test?startId=0&totalSize=10&threads=2"
curl -d "startId=0&totalSize=10&threads=2" http://localhost:8080/testProject/test
注意:
1、使用@RequestBody注解,要用post请求json格式;使用@RequestParam 注解,要用get请求,详见https://www.cnblogs.com/linjiaxin/p/9297833.html
2、传递参数时,若用object接受,long型数据,需在数字后面加上L,否则会默认为Integer型,例如"startId":0L;而用string类型或者直接指定为long型接收时,无需加L