SpringMVC中使用JSON

前台发送:

  • 传递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(); }

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

java将前端的json数组字符串转换为列表

记录下在前端通过ajax提交了一个json数组的字符串,在后端如何转换为列表。前端数据转化与请求varcontracts{id:'1',name:'yanggb合同1'},{id:'2',name:'yanggb合同2'},{id:'3',name:'yang