SpringMVC @RequestBody @RequestParam @PathVariable 等参数绑定注解详解
request 数据到handler method 参数数据的绑定所用到的注解和什么情形下使用:
http://blog.csdn.net/walkerjong/article/details/7946109 (关于更多参数绑定常用的注解)
@PathVariable
$.ajax({
url: ctx + '/management/cart/delete/'+id,
async: false,
cache: false,
type: "POST",
success: function (data) {
},
error: function (xhr) {
}
});
@ResponseBody
@RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
public int delete(@PathVariable int id){
return 0;
}
//若方法参数名称和需要绑定的uri template中变量名称不一致,需要在@PathVariable("name")指定uri template中的名称
public int delete(@PathVariable("id") int rsId){
return 0;
}
@RequestParam
A) 常用来处理简单类型的绑定,通过Request.getParameter() 获取的String可直接转换为简单类型的情况( String--> 简单类型的转换操作由ConversionService配置的转换器来完成);因为使用request.getParameter()方式获取参数,所以可以处理get 方式中queryString的值,也可以处理post方式中 body data的值;
B)用来处理Content-Type: 为 application/x-www-form-urlencoded编码的内容,提交方式GET、POST;
C) 该注解有两个属性: value、required; value用来指定要传入值的id名称,required用来指示参数是否必须绑定;
$.ajax({
url: ctx + '/management/cart/delete?id='+id,
async: false,
cache: false,
type: "POST",
success: function (data) {
},
error: function (xhr) {
}
});
@ResponseBody
@RequestMapping(value = "/delete", method = RequestMethod.POST)
public int delete(@RequestParam("id") int id){
return 0;
}
@RequestBody
该注解常用来处理Content-Type: 不是application/x-www-form-urlencoded编码的内容,而是application/json, application/xml等;
基于ajax的方法请求,将contentType设置为application/json
var cartId = 1;
$.ajax({
url: ctx + '/management/cart/delete',
async: false,
cache: false,
type: "POST",
contentType: "application/json",
data: cartId,
success: function (data) { },
error: function (xhr) { }
}); @ResponseBody
@RequestMapping(value = "/delete", method = RequestMethod.POST)
public int delete(@RequestBody Integer id) {
return 0;
}
@RequestBody接收的是一个Json对象的字符串,而不是一个Json对象。然而在ajax请求往往传的都是Json对象,后来发现用 JSON.stringify(data)的方式就能将对象变成字符串。同时ajax请求的时候也要指定contentType:"application/json" 这样就可以轻易的将一个对象或者List传到Java端,使用@RequestBody即可绑定对象或者List.
var params = [1,2,3];
$.ajax({
url: ctx + '/management/cart/delete',
async: false,
cache: false,
type: "POST",
contentType: "application/json",
data: JSON.stringify(params),
success: function (data) { },
error: function (xhr) { }
});
@ResponseBody
@RequestMapping(value = "/deletes", method = RequestMethod.POST)
public int deletes(@RequestBody List<Integer> ids) {
return 0;
}
@ResponseBody
@RequestMapping(value = "/create", method = RequestMethod.POST)
public int create(@RequestBody ExtTaskAssignmentCfg extTaskAssignmentCfg) {
UserInfo userInfo = contextService.getContextUserInfo();
extTaskAssignmentCfg.setInsman(userInfo.getUsername());
extTaskAssignmentCfg.setInsdate(new Date());
return this.extTaskAssignmentCfgService.insert(extTaskAssignmentCfg);
} var jsonObject = {};
jsonObject.id = assignmentCfgId;
jsonObject.activitiId = activityId;
jsonObject.configurationDetail = configurationDetail;
var jsonStr = JSON.stringify(jsonObject);
$.ajax({
url: url,
async: false,
cache: false,
type: "POST",
contentType:"application/json",
data: jsonStr,
success: function (data) {
},
error: function (xhr) {
alert("保存受理人分配配置失败")
}
})
@RequestMapping(value = "saveUser", method = {RequestMethod.POST }})
@ResponseBody
public void saveUser(@RequestBody List<User> users) {
userService.batchSave(users);
}
var saveDataAry=[];
var data1={"userName":"test","address":"gz"};
var data2={"userName":"ququ","address":"gr"};
saveDataAry.push(data1);
saveDataAry.push(data2);
$.ajax({
type:"POST",
url:"user/saveUser",
dataType:"json",
contentType:"application/json",
data:JSON.stringify(saveDataAry),
success:function(data){
}
});
SpringMVC @RequestBody @RequestParam @PathVariable 等参数绑定注解详解的更多相关文章
- 【转】@RequestParam @RequestBody @PathVariable 等参数绑定注解详解
@RequestParam @RequestBody @PathVariable 等参数绑定注解详解 2014-06-02 11:24 23683人阅读 评论(2) 收藏 举报 目录(?)[+] 引言 ...
- springmvc @RequestParam @RequestBody @PathVariable 等参数绑定注解详解
简介: handler method 参数绑定常用的注解,我们根据他们处理的Request的不同内容部分分为四类:(主要讲解常用类型) A.处理requet uri 部分(这里指uri templat ...
- @RequestParam @RequestBody @PathVariable 等参数绑定注解详解
文章主要讲解request 数据到handler method 参数数据的绑定所用到的注解和什么情形下使用. 简介: handler method 参数绑定常用的注解,我们根据他们处理的Request ...
- 11.@RequestParam @RequestBody @PathVariable 等参数绑定注解详解
对@RequestMapping进行地址映射讲解之后,该篇主要讲解request 数据到handler method 参数数据的绑定所用到的注解和什么情形下使用: 简介: handler method ...
- @RequestParam @RequestBody @PathVariable 等参数绑定注解详解(转)
引言: 接上一篇文章,对@RequestMapping进行地址映射讲解之后,该篇主要讲解request 数据到handler method 参数数据的绑定所用到的注解和什么情形下使用: 简介: han ...
- (转)@RequestParam @RequestBody @PathVariable 等参数绑定注解详解
引言: 接上一篇文章,对@RequestMapping进行地址映射讲解之后,该篇主要讲解request 数据到handler method 参数数据的绑定所用到的注解和什么情形下使用: 简介: han ...
- 转载:@RequestParam @RequestBody @PathVariable 等参数绑定注解详解
转载自:https://blog.csdn.net/walkerjong/article/details/7946109#commentBox 因为写的很好很全,所以转载过来 引言:接上一篇文章, ...
- Spring @RequestParam @RequestBody @PathVariable 等参数绑定注解详解
背景 昨天一个人瞎倒腾spring boot,然后遇到了一点问题,所以把这个问题总结一下. 主要讲解request 数据到handler method 参数数据的绑定,所用到的注解和什么情形下使用. ...
- @PathVariable @RequestParam @RequestBody等参数绑定注解详解
一.分类 handler method 参数绑定常用的注解,我们根据他们处理的Request的内容不同分为四类: 处理request uri 部分的注解: @PathVariable;(这里指ur ...
随机推荐
- Eclipse新建Java工程出现红色感叹号怎么解决?
安装了新版本的JDK之后,在Eclipse中新建Java工程出现红色感叹号怎么解决? 其实只要在Eclipse中重新设置一下JDK路径就行了 路径:右键Java工程>>Build Path ...
- windbg无故不显示command窗口
原文最早发表于百度空间2010-02-05 有的dump可以显示,有的不行……上网找了一通没有收获,自己搞了一下,终于在点击“window”——“cascade floating windows”后出 ...
- rabbitmq安装与高可用集群配置
rabbitmq版本:3.6.12 rabbitmq安装 1.安装openssl wget http://www.openssl.org/source/openssl-1.0.0a.tar.gz &a ...
- JS设计模式(11)中介者模式
什么是中介者模式? 中介者模式:对象和对象之间借助第三方中介者进行通信. 定义:用一个中介对象来封装一系列的对象交互,中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的 ...
- ef core一个数据库多个dbcontext
如一个项目存在多个DbCcontext且使用同一个数据库,关系: 无关联:donetcli指定具体的dbcontext类名生成migration classes 有关联:子dbcontext继承父db ...
- android to hide the keybord
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.hi ...
- Java GC机制中Minor GC/Full GC
Minor GC Young GC Full GC Major GC https://blog.csdn.net/chenleixing/article/details/46706039 内存划分为 ...
- win平台下Path变量消失问题
解决方法:2019.01.10文章转载自 李北北:https://www.jianshu.com/p/b89f0c99867e 问题描述:修改了path变量,但是环境变量中path消失,于是想再次打开 ...
- 第 8 章 容器网络 - 069 - Calico 的默认连通性
相同calico 网络之间的连通性 测试一下 bbox1 与 bbox2 的连通性: ping 成功,数据包流向如下图所示. 1)根据 bbox1 的路由表,将数据包从 cal0 发出. 2)数据经过 ...
- DFS 之 全排列
题目描述输出自然数1到n所有不重复的排列,即n的全排列,要求所产生的任一数字序列中不允许出现重复的数字. 我们可以模拟出n个盒子和n张卡片,我们需要将n张卡片分别放到n个盒子里,且每个盒子只能放1张卡 ...