SpringMVC返回JSON格式的数据:

  1 添加jar包(gson-2.8.0.jar):

        <dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.0</version>
</dependency>

   或者(jackson-databind-2.1.5.jar):

        <dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.1.5</version>
</dependency>

   2 在controller中配置注解:   

      方法的返回值就是需要的对象

      @ResponseBody会自动调用包中的方法将数据转为json格式

    @ResponseBody
@RequestMapping(value="/users-json", method=RequestMethod.GET)
public Collection<User> listjson() {
//显示列表
return userDao.getAll();
}

SpringMVC返回JSONP格式的数据:

  1 使用gson

    js只有数组,数组具有所有集合的特性

    jquery实现jsonp环境:动态生成了一个script标签,将请求转发到远程地址

    script标签必须定义到callback方法后面

@RequestMapping(value="/users-jsonp", method=RequestMethod.GET)
public void listjson(String callback, HttpServletResponse response) throws IOException {
//显示列表
Collection<User> list = userDao.getAll();
Gson gson = new Gson();
String json = gson.toJson(list);
response.getWriter().println(callback + "(" + json + ")");
}
<script>
  function showdata(data){
   console.log(data);
  }
</script>
<script src="http://localhost:8080/springmvc-02-mvn/users-jsonp?callback=showdata">
</script>

  2 使用spring内置的处理方式:

@RequestMapping(value="/user/checklogin2/{token}", produces=MediaType.APPLICATION_JSON_UTF8_VALUE)
@ResponseBody
public String checkLogin(@PathVariable String token, String callback){
MessageResult result = ssoService.getUserByToken(token);
String json = JsonUtils.objectToJson(result);
String returnStr = callback + "(" + json + ")";
return returnStr;
}

  3 使用springMVC提供的类:

@RequestMapping(value="/user/checklogin2/{token}", produces=MediaType.APPLICATION_JSON_UTF8_VALUE)
@ResponseBody
public String checkLogin(@PathVariable String token, String callback){
MessageResult result = ssoService.getUserByToken(token);
String json = JsonUtils.objectToJson(result);
String returnStr = callback + "(" + json + ")";
return returnStr;
}

SpringMVC提交JSON格式的数据:

   对表单进行json提交(使用ajax):

 $(function(){
$('#saveBtn').click(function(){
        //创建javascript对象
var row = {
username: $('#username').val(),
password: $('#password').val(),
age: $('#age').val(),
}
$.ajax({
url: 'user-json',
type: 'post' ,
contentType: 'application/json;charset=utf-8',//声明请求头的内容类型
data: JSON.stringify(row)//将js对象转为json串
});
});
});
    @RequestMapping(value="/user-json", method=RequestMethod.POST)
public String savejson(@RequestBody User user) { userDao.save(user);
return "redirect:/users";
}

Spring MVC 处理JSON | JSONP类型数据的更多相关文章

  1. Spring MVC—数据绑定机制,数据转换,数据格式化配置,数据校验

    Spring MVC数据绑定机制 数据转换 Spring MVC处理JSON 数据格式化配置使用 数据校验 数据校验 Spring MVC数据绑定机制 Spring MVC解析JSON格式的数据: 步 ...

  2. spring mvc返回json字符串数据,只需要返回一个java bean对象就行,只要这个java bean 对象实现了序列化serializeable

    1.spring mvc返回json数据,只需要返回一个java bean对象就行,只要这个java bean 对象实现了序列化serializeable 2. @RequestMapping(val ...

  3. ajax使用向Spring MVC发送JSON数据出现 org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported错误

    ajax使用向Spring MVC发送JSON数据时,后端Controller在接受JSON数据时报org.springframework.web.HttpMediaTypeNotSupportedE ...

  4. spring mvc返回json字符串的方式

    spring mvc返回json字符串的方式 方案一:使用@ResponseBody 注解返回响应体 直接将返回值序列化json            优点:不需要自己再处理 步骤一:在spring- ...

  5. Maven 工程下 Spring MVC 站点配置 (二) Mybatis数据操作

    详细的Spring MVC框架搭配在这个连接中: Maven 工程下 Spring MVC 站点配置 (一) Maven 工程下 Spring MVC 站点配置 (二) Mybatis数据操作 这篇主 ...

  6. Spring Mvc 输出Json(iwantmoon.com出品)

    原文:http://iwantmoon.com/Post/f94e49caf9b6455db7158474bab4c4dd 因为工作需要,现在要去做开放平台,考虑了多种方案之后,基本确定 下来,Htt ...

  7. Spring MVC 的json问题(406 Not Acceptable)

    原因 : 就是程序转换JSON失败. 在pom.xml 加上 <dependency> <groupId>com.fasterxml.jackson.core</grou ...

  8. Spring MVC之JSON数据交互和RESTful的支持

    1.JSON概述 1.1 什么是JSON JSON(JavaScript Object Notation,JS对象标记)是一种轻量级的数据交换格式.它是基于JavaScript的一个子集,使用了C.C ...

  9. Spring MVC返回json数据给Android端

    原先做Android项目时,服务端接口一直是别人写的,自己拿来调用一下,但下个项目,接口也要自己搞定了,我想用Spring MVC框架来提供接口,这两天便抽空浅学了一下该框架以及该框架如何返回json ...

随机推荐

  1. 使用netstat、lsof查看端口占用情况

    netstat netstat用来查看系统当前系统网络状态信息,包括端口,连接情况等,常用方式如下:   netstat -atunlp,各参数含义如下:   -t : 指明显示TCP端口 -u : ...

  2. 访问平安银行网站时出现证书问题 NET::ERR_CERT_SYMANTEC_LEGACY

    访问平安银行网站时出现证书问题 NET::ERR_CERT_SYMANTEC_LEGACY 查了资料说是 Google 取消了对 Symantec SSL 的信任,因为 Symantec 干了坏事1. ...

  3. Electron-vue 新建Demo

    vue init simulatedgreg/electron-vue Test(项目名)

  4. git push文件到远程github或者gitlab

    Git global setup git config --global user.name "luozeng" git config --global user.email &q ...

  5. swoole创建工作进程,执行滞后工作

    一,创建守候进程,因为这里不需要Server,也没有Client,数据交换通过redis进行 <?php namespace Kuba\Saas; require_once __DIR__ . ...

  6. matlab与示波器连接及电脑连接

    参考:http://blog.sina.com.cn/s/blog_4eff3a0e0100zb8h.html 最近进行了示波器的数据采集,MSO2014,openChoice软件+Tekvisa驱动 ...

  7. Django REST framework 总结(附源码剖析)

    Django 的 CBV&FBV Django FBV, function base view  视图里使用函数处理请求 url url(r‘^users/‘, views.users), v ...

  8. influxDB1.6版安装与配置(windows环境)、Jmeter+influxDB+Grafana性能监控

    influxDB1.6版安装与配置(windows环境).Jmeter+influxDB+Grafana性能监控 来源:https://blog.csdn.net/SwTesting/article/ ...

  9. <亲测>阿里云centos7安装redis

    安装redis yum install redis 启动redis systemctl start redis.service 设置redis开机启动 systemctl enable redis.s ...

  10. 同台同时多开DELPHI2007的解决办法

    Cannot create file "C:\Users\Administrator\AppData\Local\Temp\EditorLineEnds.ttr"这个问题的产生根据 ...