五、服务器端的 SpringMVC 如何返回 JSON 类型的字符串。

请求:

$("#testJson8").click(function () {
   $.ajax({
       url: "testReturnJsonValue",
       type: "post",
       success: function (result) {
           console.log(result);
       }
   });
});

1.返回单个对象

handler 方法:

@ResponseBody
@RequestMapping("/testReturnJsonValue")public Person testReturnJsonValue() {
   Person person = new Person();
   person.setName("lily");
   person.setAge(23);    return person;
}

在浏览器控制台正常打印了 Person 对象。

注意:这里没有指定 dataType。

2.返回多个对象

handler 方法:

@ResponseBody
@RequestMapping("/testReturnJsonValue")public List<Person> testReturnJsonValue() {
   List<Person> personList = new ArrayList<>();    Person person = new Person();
   person.setName("lily");
   person.setAge(23);
   Person person2 = new Person();
   person2.setName("lucy");
   person2.setAge(33);
   personList.add(person);
   personList.add(person2);    return personList;
}

在浏览器控制条正常打印了 Person 数组。

3.返回 Map

@ResponseBody
@RequestMapping("/testReturnJsonValue")public Map<String, Person> testReturnJsonValue() {
   Map<String, Person> map = new HashMap<>();    Person person = new Person();
   person.setName("lily");
   person.setAge(23);
   Person person2 = new Person();
   person2.setName("lucy");
   person2.setAge(33);    map.put("1", person);
   map.put("2", person2);    return map;
}

浏览器控制台输出:

4.在实际生产环境下的 Ajax 返回值。

封装一个返回值类型:

public class AjaxResult implements Serializable {    
public static final String RESULT_CODE_0000 = "0000";    
public static final String RESULT_CODE_0001 = "0001";    
private String code;    
private String message;    
private Object data;    
public AjaxResult() {
   }    
public String getCode() {        return this.code;
   }    
public void setCode(String code) {        this.code = code;
   }    
public String getMessage() {        return this.message;
   }    
public void setMessage(String message) {        this.message = message;
   }    
public Object getData() {        return this.data;
   }    
public void setData(Object data) {        this.data = data;
   }
}

实际使用:

@ResponseBody
@RequestMapping("/testReturnJsonValue")
public AjaxResult testReturnJsonValue() {
   AjaxResult ajaxResult = new AjaxResult();    
try {
       Map<String, Person> map = new HashMap<>();        Person person = new Person();
       person.setName("lily");
       person.setAge(23);
       Person person2 = new Person();
       person2.setName("lucy");
       person2.setAge(33);        map.put("1", person);
       map.put("2", person2);
       ajaxResult.setData(map);
       ajaxResult.setMessage("success!");
       ajaxResult.setCode(AjaxResult.RESULT_CODE_0000);
   } catch(Exception e) {
       e.printStackTrace();
       ajaxResult.setMessage("fail!");
       ajaxResult.setCode(AjaxResult.RESULT_CODE_0001);
   }    return ajaxResult;
}

六、Request Payload

(1)出现的条件:

contentType: 'application/json;charset=utf-8'

type:post

(2)具体请参看

http://xiaobaoqiu.github.io/blog/2014/09/04/form-data-vs-request-payload/

(3)建议尽量不要手动的去处理此种情况,能选用别的方式避免就尽量避免。

七、总结

主要介绍了SpringMVC 对 Ajax 的支持,对与 Ajax 数据如何组织,重点介绍了对表单的支持。

SpringMVC—对Ajax的处理(含 JSON 类型)(3)的更多相关文章

  1. SpringMVC—对Ajax的处理(含 JSON 类型)(2)

    这里编写了一个通用的类型转换器: 用来转换形如: firstName=jack&lastName=lily&gender=1&foods=Steak&foods=Piz ...

  2. SpringMVC—对Ajax的处理(含 JSON 类型)(1)

    一.首先要搞明白的一些事情. 1.从客户端来看,需要搞明白: (1)要发送什么样格式的 JSON 数据才能被服务器端的 SpringMVC 很便捷的处理,怎么才能让我们写更少的代码,如何做好 JSON ...

  3. 本文使用springMVC和ajax,实现将JSON对象返回到页面

    一.引言 本文使用springMVC和ajax做的一个小小的demo,实现将JSON对象返回到页面,没有什么技术含量,纯粹是因为最近项目中引入了springMVC框架. 二.入门例子 ①. 建立工程, ...

  4. 通过Ajax进行POST提交JSON类型的数据到SpringMVC Controller的方法

    现在在做的项目用到了SpringMVC框架,需要从前端angular接收请求的JSON数据,为了测试方便,所以直接先用AJAX进行测试,不过刚开始用平时用的ajax方法,提交请求会出现415或者400 ...

  5. springmvc通过ajax异步请求返回json格式数据

    jsp 首先创建index.jsp页面 <script type="text/javascript"> $(function () { $("#usernam ...

  6. SpringMVC——对Ajax的处理(包含 JSON 类型)

    一.首先要搞明白的一些事情. 1.从客户端来看,需要搞明白: (1)要发送什么样格式的 JSON 数据才能被服务器端的 SpringMVC 很便捷的处理,怎么才能让我们写更少的代码,如何做好 JSON ...

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

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

  8. springMVC参数绑定JSON类型的数据

    需求就是: 现在保存一个Student,并且保存Student的friend,一个student会有多个朋友,这里要传递到后台的参数是: var friends = new Array(); var ...

  9. springmvc接收JSON类型的数据

    1.在使用AJAX传递JSON数据的时候要将contentType的类型设置为"application/json",否则的话会提示415错误 2.传递的data需要时JSON类型的 ...

随机推荐

  1. Spring Cloud Stream消息总线

    Springcloud 里面对于MQ的整合一个是前一篇的消息总线一个是本文介绍的消息驱动 大体要学习这么几个知识点: 课题:SpringCloud消息驱动Stream1.什么是SpringCloud消 ...

  2. Hadoop学习1(初识hadoop)

    Hadoop生态系统的特点 1)源代码开源 2)社区活跃,参与者多 3)涉及分布式存储和计算的各方面 4)已得到企业界的验证 Hadoop构成 1) 分布式文件系统HDFS(Hadoop Distri ...

  3. Avoid nesting too deeply and return early避免嵌套太深应尽早返回

    当某个变量与多个值进行比较的时候 不要用多个if else 判断是否相等 将多个值放在数组里,然后用PHP函数in_array(mixed $needle,array $haystack),检查数组$ ...

  4. UGUI加载图片优化方法之一:打包成图集

    打包后的: 直接改变sprite中的packing tag,相同的packing tag就是同一张图集中的.改完运行会自动帮你打包

  5. [STL]vector与排序算法

    vector与算法 头文件中包含大量与 vector 相关的算法,这些算法同样适用于其它容器,比如 std::list 等. 排序(Sort) 相关函数: std::sort :普通排序 defaul ...

  6. Windows平台下Qt QOpenGL中glutSolidSphere()方法未定义的解决方法

    Windows平台下Qt中glut库的使用     用Qt中的QGLWidget窗体类中是不包括glut工具库的,难怪在myGLWidget(在我的程序中是QGLWidget的派生类)中绘制实心球体是 ...

  7. 显示隐藏文件 osx 10.10

    教程原文:http://m.blog.csdn.net/blog/i0S123tianzhuang/25736223 终端命令 Finder显示隐藏文件: defaults write com.app ...

  8. eclispe中使用 maven build启动maven项目和打包项目

    1.右键项目2.点击run as按钮 3.点击run configurations 4.配置如下: =============================加油加油加油加油加油加油========= ...

  9. iostat相关参数说明——await:平均每次设备I/O操作的等待时间 (毫秒),如果%util接近 100%,说明产生的I/O请求太多

    iostat是I/O statistics(输入/输出统计)的缩写,iostat工具将对系统的磁盘操作活动进行监视.它的特点是汇报磁盘活动统计情况,同时也会汇报出 CPU使用情况.同vmstat一样, ...

  10. LeetCode OJ:Sum Root to Leaf Numbers(根到叶节点数字之和)

    Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...