五、服务器端的 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. SQL查询顺序

    SQL查询顺序 1 FROM 2 WHERE 3 SELECT 4 ORDER BY 5 GOUP BY 6 HAVING 左外连接:查询出 left join 左边表的全部数据,left join右 ...

  2. UVA 10909 Lucky Number(树状数组+二分+YY)

    此题测试时预处理等了很久,结果470ms过了...... 题意:开始不怎么懂,结果发现是这个: 波兰裔美国数学家斯塔尼斯拉夫·乌拉姆(Stanislaw Ulam)在20世纪50年代中期开发出了另一种 ...

  3. review09

    String类在java.lang包中,由于java.lang包中的类被默认引入,所以可以直接使用String类.String对象的创建可以直接使用带字符串参数的构造方法 String s = new ...

  4. 绑定当前对象例子——Tag="{Binding}"

    <TreeView Margin="10,5,0,0" HorizontalAlignment="Left"  VerticalAlignment=&qu ...

  5. php开发工程师面必问题

    随着培训机构的增加,越来越多的php从业者流入市场,从而影响了php就业环境.公司对人才的要求越来越高,而技术者本身也要技术过硬,学习越来越多的东西,因为只有这样,你才能跑在别人前面,才不被市场抛弃, ...

  6. 解决 Firefox 火狐浏览器下载 .exe 文件卡住的问题 以及关闭测试版cache2

    解决 Firefox 火狐浏览器下载 .exe 文件卡住的问题 在firefox浏览器地址栏里输入:about:config 点“我保证会小心”,显示firefox的高级配置列表 在配置页面的搜索框里 ...

  7. 怎么样修改win7下的host文件

    由于在访问tensorflow官网时访问不了,需要修改hosts文件,然而win7下因为权限问题导致不能修改hosts文件,解决方法如下: 1.先复制hosts文件到别的地方,修改完后再覆盖回来.中间 ...

  8. LeetCode OJ:Binary Tree Right Side View(右侧视角下的二叉树)

    Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod ...

  9. LeetCode OJ:Maximum Depth of Binary Tree(二叉树最大深度)

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

  10. AOP注解式拦截

    1. 自己定义的拦截注解 package com.spring.aop; import java.lang.annotation.Documented; import java.lang.annota ...