@RequestBody和@RequestParam区别
Spring注解中能接受客户端传递过来的参数包括路径变量(url),内容变量(http body),头变量(header),COOKIE变量等几类。
1、路径变量
解析方式:@PathVariable
2、内容变量
(1)格式形如:
<header>
POST http://localhost:8090/project-web/api/client/saveDelegateRelationPart HTTP/1.1
Accept: application/json, text/javascript, */*; q=0.01
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
<body>
id=692503&consigner=fozzie&proxy=kermit&fullProxy=no&startTime=2016-07-07+00%3A00%3A00&endTime=2016-07-22+00%3A00%3A00&memo=%E6%B7%BB%E5%8A%A0%E9%83%A8%E5%88%86%E4%BB%A3%E7%90%86%E6%B5%8B%E8%AF%95&process=%5B%7B%22id%22%3A%22fixSystemFailure%3A1%3A36%22%2C%22text%22%3A%22fixSystemFailure%22%2C%22pid%22%3A%22Examples%22%7D%2C%7B%22id%22%3A%22reviewSaledLead%3A1%3A39%22%2C%22text%22%3A%22reviewSaledLead%22%2C%22pid%22%3A%22Examples%22%7D%2C%7B%22id%22%3A%22createTimersProcess%3A1%3A38%22%2C%22text%22%3A%22createTimersProcess%22%2C%22pid%22%3A%22Examples%22%7D%2C%7B%22id%22%3A%22employee-productivity-report%3A1%3A46%22%2C%22text%22%3A%22employee-productivity-report%22%2C%22pid%22%3A%22activiti-report%22%7D%2C%7B%22id%22%3A%22task-duration-report%3A1%3A49%22%2C%22text%22%3A%22task-duration-report%22%2C%22pid%22%3A%22activiti-report%22%7D%2C%7B%22id%22%3A%22process-instance-overview-report%3A1%3A47%22%2C%22text%22%3A%22process-instance-overview-report%22%2C%22pid%22%3A%22activiti-report%22%7D%2C%7B%22id%22%3A%22helpdesk-firstline-vs-escalated-report%3A1%3A48%22%2C%22text%22%3A%22helpdesk-firstline-vs-escalated-report%22%2C%22pid%22%3A%22activiti-report%22%7D%5D
解析方式: @RequestParam Map<String, String> allRequestParams
(2)格式形如:
<header>
POST http://localhost:8090/project-web/api/client/queryProxyRelation HTTP/1.1
Accept: application/json, text/javascript, */*; q=0.01
Content-Type: application/json; charset=UTF-8
<body>
{"pageIndex":0,"pageSize":10,"sortField":""}
解析方式: @RequestBody Map<String, String> requestBodyParams
总结:
即若客户端通过POST方法,在http body中传递的参数为key=value表单形式,则可用@RequestParam进行解析;若传递的参数为JSON形式,则使用@RequestBody注解进行解析。
参考:
1、 @RequestParam @RequestBody @PathVariable 等参数绑定注解详解,Truong的专栏,http://blog.csdn.net/truong/article/details/28097837,2014-06-02
2、通过Ajax post Json类型的数据到Controller,瓜王,http://www.cnblogs.com/Benjamin/archive/2013/09/11/3314576.html,2013-09-11
附:
function postSimpleData() { $.ajax({ type: "POST", url: "/Service/SimpleData", contentType: "application/json", //必须有 dataType: "json", //表示返回值类型,不必须 data: JSON.stringify({ 'foo': 'foovalue', 'bar': 'barvalue' }), //相当于 //data: "{'str1':'foovalue', 'str2':'barvalue'}", success: function (jsonResult) { alert(jsonResult); } }); } function postListString() { $.ajax({ type: "POST", url: "/Service/ListString", contentType: "application/json", dataType: "json", data: JSON.stringify({ "BuIds": ["1", "2", "3"] }), success: function (jsonResult) { alert(jsonResult); } }); } function postEmployees() { $.ajax({ type: "POST", url: "/Service/Employees", contentType: "application/json", dataType: "json", data: JSON.stringify({ "Employees": [ { "firstName": "Bill", "lastName": "Gates" }, { "firstName": "George", "lastName": "Bush" }, { "firstName": "Thomas", "lastName": "Carter" } ] }), success: function (jsonResult) { alert(jsonResult); } }); }