Spring MVC 处理JSON | JSONP类型数据
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类型数据的更多相关文章
- Spring MVC—数据绑定机制,数据转换,数据格式化配置,数据校验
Spring MVC数据绑定机制 数据转换 Spring MVC处理JSON 数据格式化配置使用 数据校验 数据校验 Spring MVC数据绑定机制 Spring MVC解析JSON格式的数据: 步 ...
- spring mvc返回json字符串数据,只需要返回一个java bean对象就行,只要这个java bean 对象实现了序列化serializeable
1.spring mvc返回json数据,只需要返回一个java bean对象就行,只要这个java bean 对象实现了序列化serializeable 2. @RequestMapping(val ...
- 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 ...
- spring mvc返回json字符串的方式
spring mvc返回json字符串的方式 方案一:使用@ResponseBody 注解返回响应体 直接将返回值序列化json 优点:不需要自己再处理 步骤一:在spring- ...
- Maven 工程下 Spring MVC 站点配置 (二) Mybatis数据操作
详细的Spring MVC框架搭配在这个连接中: Maven 工程下 Spring MVC 站点配置 (一) Maven 工程下 Spring MVC 站点配置 (二) Mybatis数据操作 这篇主 ...
- Spring Mvc 输出Json(iwantmoon.com出品)
原文:http://iwantmoon.com/Post/f94e49caf9b6455db7158474bab4c4dd 因为工作需要,现在要去做开放平台,考虑了多种方案之后,基本确定 下来,Htt ...
- Spring MVC 的json问题(406 Not Acceptable)
原因 : 就是程序转换JSON失败. 在pom.xml 加上 <dependency> <groupId>com.fasterxml.jackson.core</grou ...
- Spring MVC之JSON数据交互和RESTful的支持
1.JSON概述 1.1 什么是JSON JSON(JavaScript Object Notation,JS对象标记)是一种轻量级的数据交换格式.它是基于JavaScript的一个子集,使用了C.C ...
- Spring MVC返回json数据给Android端
原先做Android项目时,服务端接口一直是别人写的,自己拿来调用一下,但下个项目,接口也要自己搞定了,我想用Spring MVC框架来提供接口,这两天便抽空浅学了一下该框架以及该框架如何返回json ...
随机推荐
- BizDevOps — the true value proposition of workflow engines
转自:https://blog.bernd-ruecker.com/bizdevops-the-true-value-proposition-of-workflow-engines-f342509ba ...
- 几个方便进行micro frontend 开发的工具&&类库
nodejs 类库 从当前来说nodejs 的npm 偏多,因为毕竟面向的是web 编程 tailor 一个layout 服务(基于fragment 的开发方式)https://github.com/ ...
- lapis 项目添加prometheus 监控
lapis 是基于openresty 扩展的,所以直接将支持prometheus的模块构建进openresty 就可以了 我使用的是nginx-module-vts 模块 环境准备 我已经构建好了 ...
- 侃侃Thinking In Java
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/yqj2065/article/details/37074503 对于大学生,yqj2065不推荐Th ...
- laya的UI编辑器
//加载一个图集 Laya.loader.load("res/atlas/comp.json",Handler.create(this,this.onLoaderComp), Ha ...
- c# AddMonths,你了解吗?
AddMonths:找到对应月的day,如果没有则取最后一个day var d1 = new DateTime(2017, 6, 30); var d2 = d1.AddMonths(-1);//20 ...
- 深入理解 content 计数器
计数器可以说是content的重点, 因为此功能非常强大, 实用, 并且不具有可替代性, 甚至可以实现连JavaScript都不好实现的效果. 所谓css计数器效果, 就是使用CSS代码实现随元素的数 ...
- 串、串的模式匹配算法(子串查找)BF算法、KMP算法
串的定长顺序存储#define MAXSTRLEN 255,//超出这个长度则超出部分被舍去,称为截断 串的模式匹配: 串的定义:0个或多个字符组成的有限序列S = 'a1a2a3…….an ' n ...
- Jmeter录制脚本过程及Could not create script recorder报错、您的连接不是私密连接报错
转载自 https://www.cnblogs.com/wwho/p/7173172.html Jmeter录制脚本过程及Could not create script recorder报错.您 ...
- CenterOS下安装Nginx
1. 安装gcc 检查版本命令 gcc -v 安装命令 yum install gcc-c++ 2. 安装pcre 命令 yum install prce-devel 3. 安装zlib 命令 yu ...