前台发送:

  • 传递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. functiontools模块

    #!/usr/bin/env python# -*- coding:utf-8 -*-from functools import cmp_to_key a = [1, 6, 4, 5]a.sort(k ...

  2. signal() 和 sigaction()

    [摘自<Linux/Unix系统编程手册>] Unix系统提供了两种方式来改变信号处置:signal() 和 sigaction(). signal() 的行为在不同Unix实现间存在差异 ...

  3. Python_subprocess模块

    subprocess中,允许生成新的进程,连接到input/output/error管道,并获取他们的返回(状态)码,主要用于替换os.system/os.spawn*几个旧的模块和方法 subpro ...

  4. Could not locate executable null\bin\winutils.exe in the Hadoop binaries解决方式 spark运行wordcoult

    虽然可以正常运行,但是会出异常,现给出解决方法. 1.问题:   2.  问题解决: 仔细查看报错是缺少winutils.exe程序. Hadoop都是运行在Linux系统下的,在windows下ec ...

  5. jmeter访问mysql数据库

    jdbc:mysql://localhost:3306/jy?allowMultiQueries=true 如果想同时执行多条语句

  6. html-div+span-4

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. The path is not a valid path to the xxx kernel header

    在安装vmtools时无意中出现了这样的问题 1.gcc错误 Searching for GCC- The path "" is not valid path to the gcc ...

  8. Windows 7 下如何配置 java 环境变量

    安装 JDK.从Oracel官方网站上下载,下载完成后安装. http://www.oracle.com/technetwork/java/javase/downloads/jdk8-download ...

  9. ISP PIPLINE (一) BLC 以及 线性化

    what is the BlackLevel? 暗电流来源1.raw8为例,单个pixel的有效值是0~255,但是实际AD芯片的精度可能无法将电压值很小的一部分转换出来,芯片厂会刻意添加一个固定的偏 ...

  10. WPFToolkit DataGrid 使用介绍zz

      首先:这版本需要.NetFrameWork SP1的支持 WPF Toolkit - February 2010 Release 下载地址:http://wpf.codeplex.com/rele ...