前台发送:

  • 传递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. document.createDocumentFragment()运行效率

    createDocumentFragment作用是什么? 快速响应,提高效率,提升用户体验. 调用document.body.append(),每调用一次都要刷新页面 一次.效率就低了. 用docum ...

  2. mysql数据类型(三)

    MySQL 数据类型 MySQL中定义数据字段的类型对你数据库的优化是非常重要的. MySQL支持多种类型,大致可以分为三类:数值.日期/时间和字符串(字符)类型. 数值类型 MySQL支持所有标准S ...

  3. 一次BurpSuite无法抓https包定位

  4. redis 在 php 中的应用

    一.redis 在 php 中的应用(Key篇) 二.redis 在 php 中的应用(String篇) 三.redis 在 php 中的应用(Hash篇) 四.redis 在 php 中的应用(Li ...

  5. ip转城市接口,ip转省份接口,ip转城市PHP方法

    新浪接口(速度快) $url = 'http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip='.$ip; $arr ...

  6. 通过mysql-proxy映射外网访问内网数据库

    配置教程: 转自:http://www.centoscn.com/mysql/2015/0107/4437.html centos安装mysql-proxy mysql-proxy的用处就不再说了 m ...

  7. 009 spring boot中文件的上传与下载

    一:任务 1.任务 文件的上传 文件的下载 二:文件的上传 1.新建一个对象 FileInfo.java package com.cao.dto; public class FileInfo { pr ...

  8. 关于thinkphp3自动完成的笔记

    当我在前台传入的主键id与字段表的主键id值时,在更新时tp总是判断为新增的状态(解决办法:将前台的表单主键名保持和数据表主键id名一只,手动创建数据) create时是先获取主键id判断'$type ...

  9. CSS规范 - 分类方法

    CSS文件的分类和引用顺序 通常,一个项目我们只引用一个CSS,但是对于较大的项目,我们需要把CSS文件进行分类. 我们按照CSS的性质和用途,将CSS文件分成“公共型样式”.“特殊型样式”.“皮肤型 ...

  10. docker+springboot+elasticsearch+kibana+elasticsearch-head整合(详细说明 ,看这一篇就够了)

    一开始是没有打算写这一篇博客的,但是看见好多朋友问关于elasticsearch的坑,决定还是写一份详细的安装说明与简单的测试demo,只要大家跟着我的步骤一步步来,100%是可以测试成功的. 一.  ...