基于springframework框架的不同网络请求request方式传参
一、get方式
1.在url路径上传递参数,形如 /getTask/{taskId} 如/getTask/111
@RequestMapping(value = {"/getTask/{taskId}"}, method = RequestMethod.GET)
public ResponseResult getTask(@PathVariable("taskId") String taskId) {...}
2.在url后面以?形式传递参数,形如/getTask?taskId=111
@RequestMapping(value = {"/getTask"}, method = RequestMethod.POST) public ResponseResult getTask(@RequestParam("taskId") String taskId) {...} 或者 @RequestMapping(value = {"/getTask"}, method = RequestMethod.POST) public ResponseResult getTask(@RequestParam Map<String, Object> map) {...}
二、post方式
1.form表单提交数据,使用@RequestParam接收参数值,与get中的问号传参相同
@RequestMapping(value = {"/getTask"}, method = RequestMethod.POST) public ResponseResult getTask(@RequestParam("taskId") String taskId) {...} 或者 @RequestMapping(value = {"/getTask"}, method = RequestMethod.POST) public ResponseResult getTask(@RequestParam Map<String, Object> map) {...}
2.json格式传递参数(非表单)
@RequestMapping(value = {"/getTask"}, method = RequestMethod.POST) public ResponseResult getTask(@RequestBody String jsonStr) {...} @RequestMapping(value = {"/getTask"}, method = RequestMethod.POST) public ResponseResult getTask(@RequestBody JSONObject bodyparams) {...} @RequestMapping(value = {"/getTask"}, method = RequestMethod.POST) public ResponseResult getTask(@RequestBody Task task) {...}