五、服务器端的 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. java定时任务Quartz Demo(2.X)

    直接上代码 public class HelloQuartz implements Job{ @Override public void execute(JobExecutionContext Jec ...

  2. redis集群使用Java工具类(Java jedis集群工具类)

    package com.xiaomi.weather.vote.webservices.util.redisCache; import com.google.common.base.Strings; ...

  3. jQuery颜色面板插件

    /** * jQuery颜色面板插件 * * 使用方法:input框的id默认为inputObj,button框的id默认为btnObj,也可以自定义aaa,bbb * 1.初始化颜色面板:$.col ...

  4. DataX-ElasticSearch(写)

    DataX写入ElasticSearch 1 快速介绍 数据导入elasticsearch的插件 2 实现原理 使用elasticsearch的rest api接口, 批量把从reader读入的数据写 ...

  5. 免配置环境变量使用Tomcat+设置项目主页路径为http://localhost:8080+修改tomcat端口号

    一.免配置jdk JAVA_HOME和tomcat  CATALINA_HOME环境变量使用tomcat 众说周知,使用tomcat需要有java环境,一般情况下需要配置jdk和tomcat的路径到w ...

  6. 利用PushbackReader读取文件中某个字符串之前的内容

    package File; import java.io.FileReader; import java.io.IOException; import java.io.PushbackReader; ...

  7. Linux脚本基础

    1.文本编辑 命令模式快捷键命令 命令 作用 dd 删除(剪切)光标所在整行. 5dd 删除(剪切)从光标处开始的5行. yy 复制光标所在整行. 5yy 复制从光标处开始的5行. n 显示搜索命令定 ...

  8. uva 10739 dp

    https://vjudge.net/problem/UVA-10739 和昨天的那个回文串几乎一样只是加了条件限制,可以随意增删以及替换. #include<iostream> #inc ...

  9. Https---SSL协议

    ssl协议的起源和历史我就不再多说了,就是那个Netscape 网景公司开发的,它的作用主要是提供了一种安全传输方式,我们知道网上有很多的时候需要我们去输入用户名和密码,那么假设我们自己的电脑防病毒还 ...

  10. python里混淆矩阵 左下角为漏报,右上角为误报

    1为黑样本,0为白样本: Counter({1: 105, 0: 95}) check counter!confusion_matrix:[[83 12(预测值为1,实际为0,误报)] [15(预测值 ...