springMVC 接收json字符串参数
/**
前台js拼接了一个数组 myparam = [a,b,c]; 在ajax中直接 {"myparam":JSON.stringify(myparam)} 传入springMVC的controller中,在接收端可以这样接收
*/
@RequestMapping(value="/aaa")
@ResponseBody
public void aaa(@RequestParam(value="myparam")Object myparam ){ //@RequestParam(value="myparam") 也可以不写,默认就是它
Gson gson = new Gson();
List mylist = gson.fromJson(myparam.toString(),List.class);
for(int i;i<mylist.size();i++){
String item = mylist.get(i).toString(); //得到数组的每一个元素
} }
上面也可以这样写:
/**
前台js拼接了一个数组 myparam = [a,b,c]; 在ajax中直接 {"myparam":myparam} 传入springMVC的controller中,在接收端可以这样接收
*/
@RequestMapping(value="/aaa")
@ResponseBody
public void aaa(String[] myparam ){ //这种写法时,前台不能传json字符串,要直接传数组对象 myparam 不用转JSON.stringify for(int i;i<myparam.length;i++){
myparam[i] //得到数组的每一个元素
} }
//另外如果用ajaxSubmit提交时,可以直接把 myparam = [a,b,c] 中的myparam扔到一个input框的val中,即:$("#inputArr").val(myparam)
//然后再直接ajaxSubmit表单提交,效果和上面一样 myparam 会作为一个数组对象被传递到后台。
//也就是说不是input框只能提交字符串,扔个 数组对象 进去也是可以的。照样可以提交对象。
--------------------------------------------------------------------------------
下面是传递其他类型的json数据,可以参考下
//js定义json对象
var username = $("#username").val();
var password = $("#password").val();
var json = {
"username" : username,
"password" : password
};
$.ajax({
url : "jsontest",
type : "POST",
async : true,
contentType : "application/json",
data : JSON.stringify(json),
dataType : 'json',
success : function(data) {
if (data.userstatus === "success") {
$("#errorMsg").remove();
} else {
if ($("#errorMsg").length <= 0) {
$("form[name=loginForm]").append(errorMsg);
}
}
}
});
@RequestMapping("/jsontest")
public void test(@RequestBody(required=true) Map<String,Object> map ){
String username = map.get("username").toString();
String password = map.get("password").toString();
System.out.println("username: " + username);
System.out.println("password: " + password);
}
这种用map接收的方式要求后台必须用@RequestBody注解,但是这样要求ajax必须用
contentType : "application/json"
方式,当同一次请求除了传递json类型还传递许多其他的字符串参数时,就或报400错误。这个是个硬伤,要用这种方式,Controller层方法中只能接收一个Json类型的参数,不能再有其他类型的参数。
----------------------------------
也可以参考下面:
@Controller
@RequestMapping("/admin/Obj")
public class ObjAction {
/**
* 前端操作与上面相同
* @return
*/
@RequestMapping(value = "/updateAttr")
@ResponseBody
public String updateAttr(@RequestBody Map<String, String> map) {
if(map.containsKey("id"){
Integer id = Integer.parseInt(map.get("id"));
}
if(map.containsKey("objname"){
String objname = map.get("objname").toString();
}
if(map.containsKey("pid"){
Integer pid = Integer.parseInt(map.get("pid"));
}
// 操作 ...
return "success";
}
}
------------------
还可以参考这个:
js代码:
<head>
<title>submitUserList_3</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<script language="JavaScript" src="/js/jquery.min.js" ></script>
<script language="JavaScript" src="/js/jquery.json.min.js" ></script>
<script type="text/javascript" language="JavaScript">
function submitUserList_3() {alert("ok");
var customerArray = new Array();
customerArray.push({id: "1", name: "李四", pwd: "123"});
customerArray.push({id: "2", name: "张三", pwd: "332"});
$.ajax({
url: "/user/submitUserList_3",
type: "POST",
contentType : 'application/json;charset=utf-8', //设置请求头信息
dataType:"json",
//data: JSON.stringify(customerArray), //将Json对象序列化成Json字符串,JSON.stringify()原生态方法
data: $.toJSON(customerArray), //将Json对象序列化成Json字符串,toJSON()需要引用jquery.json.min.js
success: function(data){
alert(data);
},
error: function(res){
alert(res.responseText);
}
});
}
</script>
</head>
java代码:
@RequestMapping(value = "/submitUserList_3", method ={RequestMethod.POST})
@ResponseBody
public String submitUserList_3(@RequestBody List<User> users)
throws Exception{
String result = "";
if(users == null || users.size() <= 0){ return "No any ID.中文"; }
result = this.showUserList(users);
return result;
}
springMVC 接收json字符串参数的更多相关文章
- api controller 接口接收json字符串参数
{"data":{"alarmRepeatTimes":2,"currentMode":1,"moduleResetTimeout ...
- springmvc接收json注意事项
在以前使用SpringMvc框架时,在接受json数据时碰到了一些奇怪的问题.这里记录下来,方便以后查阅. 1. data 里写json对象 , 即该json数据没有被单(双)引号包住 ...
- SpringMVC接收集合页面参数
SpringMVC接收集合页面参数 Spring MVC在接收集合请求参数时,需要在Controller方法的集合参数里前添加@RequestBody,而@RequestBody默认接收的enctyp ...
- spring接收json字符串的两种方式
一.前言 前几天遇到一个问题,前端H5调用我的springboot一个接口(post方式,@RequestParameter接收参数),传入的参数接收不到.自己测试接口时使用postman的form- ...
- AJAX发送json,SpringMVC 接收JSON,@RequestBody
需求:JQuery ajax前台,采用 POST请求 发送json,后台使用SpringMVC接收json并处理 前台: $.ajax({ url:"请求地址", type:&qu ...
- SpringMVC接收复杂集合参数
Spring MVC在接收集合请求参数时,需要在Controller方法的集合参数里前添加@RequestBody,而@RequestBody默认接收的enctype (MIME编码)是applica ...
- SpringMVC接收json数组对象
最近帮一个妹子解决一个需求,就是前台使用ajax传三个相同的对象,再加一个form表单对象.然后遇到各种问题,终于解决了,@RequestBody接收Json对象字符串 以前,一直以为在Spring ...
- 原生ajax接收json字符串(简单介绍)
什么是json? JSON的全称是 Javascript Object Notation(javascript对象表示法),是基于javascript对象字面量,如果单从眼睛看,JSON里的数据是被保 ...
- SpringMVC接收对象数组参数进行封装
前台代码:注意.contentType : "application/json; charset=utf-8",必须要设置,只有这样SpringMVC才认识这个json数组参数 f ...
随机推荐
- CentOS6.9下升级默认的OpenSSH操作记录(升级到OpenSSH_7.6p1)
近期对IDC机房服务器做了一次安全漏洞扫描,漏扫结果显示服务器的OpenSSH版本太低(CentOS6默认是OpenSSH_5.3p1),存在漏洞隐患,安全部门建议升级到OpenSSH_7.6p1.升 ...
- L2-027. 名人堂与代金券
链接:名人堂与代金券 在比赛中这题只得了2分,赛后发现原来strcmp函数并不是只返回-1,0,1三种,而是返回正数负数0 但是在我的电脑上一般就是返回前三种,只是用后面的三种更稳妥点 都怪我基础不扎 ...
- 五子棋游戏SRS文档
SRS技术文档,包括对SRS的解释说明.SRS描述规范.软件需求规格说明书(SRS,Software Requirement Specification)是为了软件开发系统而编写的,主要用来描 ...
- SVN上传下载项目
1 本机安装svn服务器 1.1 创建新库 创建分支, Trunk:主干, Tags:分支节点 Branches:分支 比如说我们现在tomcat出了6.0以后已经定版了,但是还得继续开发呀,开 ...
- PAT 1003 我要通过!
https://pintia.cn/problem-sets/994805260223102976/problems/994805323154440192 “答案正确”是自动判题系统给出的最令人欢喜的 ...
- PAT 1009 说反话
https://pintia.cn/problem-sets/994805260223102976/problems/994805314941992960 给定一句英语,要求你编写程序,将句中所有单词 ...
- python 3以上版本使用pickle.load读取文件报UnicodeDecodeError: 'ascii' codec can't decode byte 0x8b in position 6
python 3以上版本使用pickle.load读取文件报UnicodeDecodeError: 'ascii' codec can't decode byte 0x8b in position 6 ...
- adminlte前端框架从入门到精通
第一 下载 admintle的下载地址为: https://github.com/almasaeed2010/AdminLTE/releases 参考实例文件代码: 例如:AdminLTE-2.4.3 ...
- js對象構造
創建對象的3種方式: 1. var a=new Object() a.attributes=“1”: 2. var a={attributes:"1",aa:"2&quo ...
- BZOJ[Usaco2017 Jan]Promotion Counting——线段树合并
题目描述 The cows have once again tried to form a startup company, failing to remember from past experie ...