前台发送:

  • 传递JSON对象
function requestJson(){
$.ajax.({
type : “post”,
url : “${pageContext.request.contextPath}/testJson/responseJson”,
contextType : “application/x-www-form-urlencoded;charset=utf-8”,//默认值
data : ‘{“username” : “name “, “gender” : “male”}’,
dataType : “json”,
success:function(data){
console.log(“服务器处理过的用户名是:” + data.username);
console.log(“服务器处理过的性别是:” + data.gender);
}
});
}
  • 传送JSON字符串
function requestJson(){
var json = {“username” : “name “, “gender” : “male”};
var jsonstr = JSON.stringify(json);//json对象转换json字符串
$.ajax.({
type : “post”,
url : “${pageContext.request.contextPath}/testJson/responseJson”,
contextType : “application/json;charset=utf-8’
traditional:true,//这使json格式的字符不会被转码,
data : jsonstr,
dataType : “json”,
success:function(data){
console.log(“服务器处理过的用户名是:” + data.username);
console.log(“服务器处理过的性别是:” + data.gender);
}
});
}

下面介绍以上参数的含义:

type:Http请求的方式,一般使用POST和GET

url:请求的url地址,也就是我们所要请求的Controller的地址

contextType:这里要注意,如果使用的是key/value值,那么这个请求的类型应该是application/x-www-form-urlencoded/charset=utf-8,该值也是默认值。

如果是JSON格式的话应该是application/json;charset=utf-8

traditional:若一个key是个数组,即有多个参数的话,可以把该属性设置为true,这样就可以在后台可以获取到这多个参数。

data:要发送的JSON字符串

success:请求成功之后的回调函数

error:请求失败之后的回调函数

SpringMVC需要的jar包:jackson-core-asl-x.x.xx.jar、jackson-mapper-asl-x.x.xx.jar

SpringMVC配置文件中添加:

后台接收

  • 接收JSON对象

    Controller控制器中的方法

    ①直接获取就行
@RequestMapping("/requestJson")
public @ResponseBody Person requestJson(Person p) {
System.out.println("json传来的串是:" + p.getGender() + " " + p.getUserName() + " " +p.isAdalt());
p.setUserName(p.getUserName().toUpperCase());
return p;
}
Person的实体类:
public class Person {
private String userName;
private String gender;
private boolean adalt;
}
  • 接收JSON字符串
@RequestBody(接收的是JSON的字符串而非JSON对象),注解将JSON字符串转成JavaBean
@ResponseBody注解,将JavaBean转为JSON字符串,返回到客户端。具体用于将Controller类中方法返回的对象,通过HttpMessageConverter接口转换为指定格式的数据,比如JSON和XML等,通过Response响应给客户端。produces=”text/plain;charset=utf-8”设置返回值。
②把JSON字符串发送过来,使用注解@RequestBody接收String字符串之后,再解析出来
@RequestMapping(value = "testAjax.action",method = RequestMethod.POST)
public void testAjax(@RequestBody String jsonstr){
JSONObject jsonObject = JSONObject.parseObject(jsonstr);//将json字符串转换成json对象 String age = (String) jsonObject.get("age");//获取属性
System.out.println(age);
} ③当然也可以使用Map集合
@RequestMapping("/jsontest")
public void test(@RequestBody Map map){
String username = map.get("username").toString();
String password = map.get("password").toString();
}

SpringMVC中使用JSON的更多相关文章

  1. SpringMVC中使用Json传数据

    在web项目中使用Json进行数据的传输是非常常见且有用的,在这里介绍下在SpringMVC中使用Json传数据的一种方法,在我的使用中,主要包括下面四个部分(我个人喜好使用maven这类型工具进行项 ...

  2. SpringMVC中返回JSON时乱码的解决方案

    springMVC中返回JSON会出现乱码,解决如下: produces = "text/html;charset=UTF-8" @ResponseBody @RequestMap ...

  3. SpringMVC中响应json数据(异步传送)

    1.首先导入3个jar包: jackson-annotations-2.1.5.jar jackson-core-2.1.5.jar jackson-databind-2.1.5.jar JSON所需 ...

  4. SpringMVC中出现" 400 Bad Request "错误(用@ResponseBody处理ajax传过来的json数据转成bean)的解决方法

    最近angularjs post到后台 400一头雾水 没有任何错误. 最后发现好文,感谢作者 SpringMVC中出现" 400 Bad Request "错误(用@Respon ...

  5. springMvc中406错误解决,springMvc使用json出现406 (Not Acceptable)

    springMvc中406错误解决, springMvc使用json出现406 (Not Acceptable) >>>>>>>>>>> ...

  6. SpringMVC 中整合之JSON、XML

    每次看到好的博客我就想好好的整理起来,便于后面自己复习,同时也共享给网络上的伙伴们! 博客地址: springMVC整合Jaxb2.xStream:  http://www.cnblogs.com/h ...

  7. Springmvc 中org.springframework.http.converter.json.MappingJackson2HttpMessageConverter依赖jackson包

    1,问题详情:Spring使用4.3.5.Release版本后 在SpringMvc配置文件中配置json 解析器后出现报错信息 [org.springframework.web.context.Co ...

  8. SpringMVC中使用Ajax POST请求以json格式传递参数服务端通过request.getParameter("name")无法获取参数值问题分析

    SpringMVC中使用Ajax POST请求以json格式传递参数服务端通过request.getParameter("name")无法获取参数值问题分析 一:问题demo展示 ...

  9. Ajax json交互和SpringMVC中@RequestBody

    Ajax json交互和SpringMVC中@RequestBody 标签: 背景 自己提供出去得接口中参数设置为@RequestBody VipPromotionLog vipPromotionLo ...

随机推荐

  1. P4715 「英语」Z 语言

    题解: 平衡树维护hash值 为了支持加入删除操作 x*base^y 其中y为他是第k大 同一般的维护方法,我们不用对每个节点维护他的hash值 而是只用记录他的x值(他的位置) 然后通过updata ...

  2. Nginx配置项优

    1.nginx运行工作进程个数,一般设置cpu的核数或者核心数x2 如果不了解cpu的核数,可以top命令之后按1看出来,也可以查看/proc/cpuinfo文件. [root@localhost~] ...

  3. makefile:n: *** missing separator. Stop

    makefile has a very stupid relation with tabs, all actions of every rule are identified by tabs .... ...

  4. 基于spring security 实现前后端分离项目权限控制

    前后端分离的项目,前端有菜单(menu),后端有API(backendApi),一个menu对应的页面有N个API接口来支持,本文介绍如何基于spring security实现前后端的同步权限控制. ...

  5. 请推荐几个asp.net下做网站的好的开源框架

    1.We7 CMS We7 CMS是由西部动力开发的一款充分发掘互联网Web2.0(如博客.RSS等)的信息组织优势,将其理念利用到政府企事业网站的构建.组织.管理中的网站建设和管理方面的产品. 系统 ...

  6. IIS:另一个程序正在使用此文件进程无法访问。

    启动网站时,遇到这个错误,一般是端口已经被占用,更换一个空闲端口即可. 通过以下命令可查询 根据最后一列的数字在任务管理器中可查看被哪个程序占用了

  7. 解决 js setTimeout 传递带参数的函数无效果

    最近 js  用到 setTimeout 递归调用 刷新进度  setTimeout ("getProgress(name,type)", 3000) ; 发现getProgres ...

  8. checkbox jquery操作总结

    $('input[name="myCheckbox"]').prop('checked','true'); // 全选 $('input[name="myCheckbox ...

  9. linux 学习笔记 cpio命令

    1 文件或目录打包 打包有如下多种情况 A>包含子目录打包 find /usr/lib -print /cpio -o >/uo/temp1.cpio 将/usr/lib目录下的文件与子目 ...

  10. javascript闭包和this对象

    闭包(closure)是Javascript语言的一个难点,也是它的特色,很多高级应用都要依靠闭包实现. 一.变量的作用域 要理解闭包,首先必须理解Javascript特殊的变量作用域. 变量的作用域 ...