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 ...
随机推荐
- Linq中left join之多表查询
using System; using System.Collections; using System.Collections.Generic; using System.Data; using S ...
- zabbix--3.0--3
使用JMX监控jvm vim /usr/local/tomcat/bin/catalina.sh 添加如下内容 CATALINA_OPTS="$CATALINA_OPTS -Dcom ...
- 基于MVC4+EasyUI的Web开发框架形成之旅(4)--附件上传组件uploadify的使用
大概一年前,我还在用Asp.NET开发一些行业管理系统的时候,就曾经使用这个组件作为文件的上传操作,在随笔<Web开发中的文件上传组件uploadify的使用>中可以看到,Asp.NET中 ...
- Asp.Net Grieview Eval 绑定数据 调用JS事件
<asp:TemplateField ItemStyle-HorizontalAlign="Center"> <ItemTemplate> <asp: ...
- jQuery位置操作
position();获取当前标签相对于最近一个父标签中有positon:relative属性的位置. height();标签纯高度 innerHeight();标签内边距padding加上纯高度 o ...
- CentOs6.7 python2.6升级到2.7.11
1.查看当前python的版本 #python -V Python 2.6.6 2.下载Python-2.7.11 wget https://www.python.org/ftp/python/2.7 ...
- 查找Python项目依赖的库并生成requirements.txt
使用pip freeze pip freeze > requirements.txt 这种方式配合virtualenv 才好使,否则把整个环境中的包都列出来了. 使用 pipreqs 这个工具的 ...
- 开IE时 暴卡
待打开IE后,在“工具”-“管理加载项”中禁用所有加载项.
- windows server core 远程桌面
要允许其它计算机透过远程桌面登入Server Core主机,我们需要先调整注册机码,并开启对应的防火墙端口号首先,我们开启登录编辑程序(regedit.exe),找到HKEY_LOCAL_MACHIN ...
- 黄聪:bootstrap中模态框modal在苹果手机上会失效
bootstrap中模态框在苹果手机上会失效 可将代码修改为<a data-toggle="modal" data-target="#wrap" hre ...