五、服务器端的 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. POJ 1635 Subway tree systems (树的最小表示法)

    题意:一串01序列,从一个点开始,0表示去下一个点,1表示回到上一个点,最后回到起点,遍历这棵树时每条边当且仅当走2次(来回) 给出两串序列,判断是否是同一棵树的不同遍历方式 题解:我们把每一个节点下 ...

  2. 【转】Android ImageView的scaleType属性与adjustViewBounds属性

    ImageView的scaleType的属性有好几种,分别是matrix(默认).center.centerCrop.centerInside.fitCenter.fitEnd.fitStart.fi ...

  3. div css 练习2

    index.html <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-eq ...

  4. Java中的算术运算符

    算术运算符主要用于进行基本的算术运算,如加法.减法.乘法.除法等. Java 中常用的算术运算符: 其中,++ 和 -- 既可以出现在操作数的左边,也可以出现在右边,但结果是不同滴 例1: 运行结果: ...

  5. 【转】ORACLE的数据类型

    原文;http://linjian004.bokee.com/3916067.html 常用的数据库字段类型如下: 字段类型 中文说明 限制条件 其它说明 CHAR 固定长度字符串 最大长度2000 ...

  6. thinkphp中如何使用phpspreadsheet插件

    thinkphp中如何使用phpspreadsheet插件 一.总结 一句话总结:多百度,百度什么都有 1.thinkphp中用composer安装的插件的命名空间是什么? use PhpOffice ...

  7. java:system根据输入的内容,然后输出(字节流)

    把输入的内容输出来:根据system.in的内容System.out.println输出出来 都是字节流,的形式: //限制读取的字符长度 //字节流 InputStream ips = System ...

  8. 目标检测 — 计算IOU

    1.IOU=交集/并集 #include<iostream> #include<algorithm> #include<stdio.h> #include < ...

  9. 按返回键退出程序但不销毁代码,像QQ一样,后台运行

    @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BA ...

  10. python--基本代码规范

    python代码规范:一.标识符 所谓的标识符就是对变量.常量.函数.类等对象起的名字 python语言在任何场景都严格区分大小写!!! python对于标识符的命名有如下规定: 第一个字符必须是字母 ...