前台发送:

  • 传递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. 精简版自定义 jquery

    function $(id) { var el = 'string' == typeof id ? document.getElementById(id) : id; el.on = function ...

  2. Ant之build.xml详解

    Ant之build.xml详解 关键字: ant build.xml Ant的概念 可能有些读者并不连接什么是Ant以及入可使用它,但只要使用通过Linux系统得读者,应该知道make这个命令.当编译 ...

  3. Python学习(十二) —— 面向对象

    一.初识面向对象 面向过程的核心是过程,流水线思维,过程即解决问题的步骤,面向过程的设计就好比精心设计好一条流水线,考虑周全什么时候处理什么东西. 优点:极大地降低了写程序的复杂度,只需要顺着要执行的 ...

  4. Anslib 使用错误归纳

    一.遇到问题 [root@localhost ansible]# ansible test43 -m ping -kSSH password: 192.168.30.43 | FAILED! => ...

  5. html5的audio实现高仿微信语音播放效果

    效果图 前台大体呈现效果图如下: 点击就可以播放mp3格式的录音.点击另外一个录音,当前录音停止! 思路 关于播放动画,这个很简单,我们可以用css3的逐帧动画来实现.关于逐帧动画,我之前的文章也写过 ...

  6. fillder---安装证书(抓取https)

    1.fillder中设置如下(不用管为啥,按照这个设置就对了) 2.需要在手机上安装证书,有两种方式 方式一:fillder直接导出到手机中 方式二:手机上直接下载 打开手机自带浏览器:网址输入[XX ...

  7. 51Nod1317 相似字符串对 容斥原理 动态规划

    原文链接https://www.cnblogs.com/zhouzhendong/p/51Nod1317.html 题目传送门 - 51Nod1317 题意 称一对字符串(A,B)是相似的,当且仅当满 ...

  8. js获取元素提示信息

    js获取元素提示信息 var date=$("#date").attr('placeholder'); js修改元素的提示信息 $("#date").attr( ...

  9. Python replace() 和 re.sub() 字符串字符替换

    Python replace() 和 re.sub() 字符串字符替换 replace() testStr = 'aa:bb[cc' testStr.replace(':','_') 每次只能替换一个 ...

  10. POJ 2455 Secret Milking Machine 【二分】+【最大流】

    <题目链接> 题目大意: FJ有N块地,这些地之间有P条双向路,每条路的都有固定的长度l.现在要你找出从第1块地到第n块地的T条不同路径,每条路径上的路段不能与先前的路径重复,问这些路径中 ...