spring之json数据的接受和发送
配置spring对json的注解方式。
<!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" >
<property name="messageConverters">
<list >
<ref bean="mappingJacksonHttpMessageConverter" />
</list>
</property>
</bean> <!-- 处理JSON数据转换的 -->
<bean id="mappingJacksonHttpMessageConverter"
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
<value>application/json; charset=UTF-8</value>
</list>
</property>
</bean>
Controller层使用了@requestBody和@responseBody注解
/**
* 接受json参数,返回json数据
* @param area
* @param request
* @param response
* @return
* @throws Exception
*/
@ResponseBody
@RequestMapping("/getJson")
public Map<String, Object> getJson(@RequestBody Area area, HttpServletRequest request,HttpServletResponse response) throws Exception{
Map<String, Object> resultMap = new HashMap<String, Object>();
try {
System.out.println(area.getName());
Area entity = areaService.queryById(area.getId());
responseOk(resultMap, entity);
} catch (Exception e) {
responseError(resultMap, Constant.FAILURE_MESSAGE, e);
}
return resultMap;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script type="text/javascript" src="/./resources/js/jquery-1.8.3.min.js"></script>
<script type="text/javascript"> $(function(){
$(".areajson").click(function(){
var id=$(this).attr("id");
var areaNmae=$(this).attr("areaNmae");
var level=$(this).attr("level");
var json = JSON.stringify({"id":id,"name":areaNmae,"level":level});//发送json数据
jQuery.ajax({
url:"/area/getJson",
type:"post",
dataType:"json",
data:json,
contentType:'application/json;charset=UTF-8',//使用@requestBody必须设置contentType为json才能接受到Json数据
success:function(jsondata){
alert(jsondata.result.name);
}
})
}) })
</script>
<title>Insert title here</title>
</head>
<body>
#foreach($obj in $!list)
<a href="javascript:;"><span id="$!obj.id" class="areajson" areaNmae="$!obj.name" level="$!obj.level">$!obj.name</span></a>
#end
</body>
</html>
我在这里遇到两个问题:1、用$.ajax({}),使用$一直报错,但是用jQuery就不报错,暂时还不知道原因。2、必须设置contentType,不然会报415错误。
这样就完成spring接受和返回json数据了。很简单的一个例子。
还有一种就是使用printWriter来返回json。
response.setContentType("text/plain");
response.setHeader("Cache-Control", "no-cache");
response.setCharacterEncoding("UTF-8");
try {
PrintWriter writer = response.getWriter();
writer.print(Json.toJson(resultMap));
} catch (IOException e) {
e.printStackTrace();
}
这里的Json是org.nutz.json.Json;将map转化为json数据。Json.toJson接受的参数是Object。所有也可以是数组、集合、对象。
post提交方式,以上面那种参数提交的话,他的请求体是这样的
如果以这种方式提交参数。
data:{"id":id,"name":areaNmae,"level":level}

可以看看Request Payload。
Json数据现在在APP或者是网站上都非常有用。得到json数据,要具体的数据只要一直.过去就行了。例如a.b.c.d就能获取到d的值。
spring之json数据的接受和发送的更多相关文章
- 使用jQuery解析JSON数据(由ajax发送请求到php文件处理数据返回json数据,然后解析json写入html中呈现)
在上一篇的Struts2之ajax初析中,我们得到了comments对象的JSON数据,在本篇中,我们将使用jQuery进行数据解析. 我们先以解析上例中的comments对象的JSON数据为例,然后 ...
- Spring MVC Json数据传递
json是一种常见的传递格式,是一种键值对应的格式.并且数据大小会比较小,方便传递.所以在开发中经常会用到json. 首先看一下json的格式: {key1:value1,key2:value2} 每 ...
- Spring返回json数据
第一种形式:使用注解@ResponseBody @RequestMapping(value = "/admin/jq", method = RequestMethod.GET) @ ...
- (转)获取 request 中用POST方式"Content-type"是"application/x-www-form-urlencoded;charset=utf-8"发送的 json 数据
request中发送json数据用post方式发送Content-type用application/json;charset=utf-8方式发送的话,直接用springMVC的@RequestBody ...
- 获取 request 中用POST方式"Content-type"是"application/x-www-form-urlencoded;charset=utf-8"发送的 json 数据
request中发送json数据用post方式发送Content-type用application/json;charset=utf-8方式发送的话,直接用springMVC的@RequestBody ...
- 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数据的传输与接受
本篇文章写给刚接触SpingMVC的同道中人,虽然笔者本身水平也不高,但聊胜于无吧,希望可以给某些人带来帮助 笔者同时再次说明,运行本例时,需注意一些配置文件和网页脚本的路径,因为笔者的文件路径与读者 ...
- angular2^ typescript 将 文件和Json数据 合并发送到服务器(2.服务端)
nodejs 中使用框架 express web框架 multer 文件接受 直接贴代码了,我就不解释了 "use strict"; exports.__esModule = tr ...
- Spring使用Jackson处理json数据
1.搭建SpringMVC+Spring环境 2.配置web.xml.SpringMVC-config.xml <?xml version="1.0" encoding=&q ...
随机推荐
- 【转】http://www.cnblogs.com/yuzukwok/p/3884377.html
来自:http://www.cnblogs.com/yuzukwok/p/3884377.html An Introduction to Xamarin.Forms 来源:http://develop ...
- Python学习笔记——基础篇【第五周】——random & time & datetime模块
random模块 随机数 mport random print random.random() print random.randint(1,2) print random.randrange(1,1 ...
- wpf xmal基础
1.名称空间的引用 比如想使用System.Windows.Controls名称空间 首先需要把改名称空间所在的程序集presentationFramework.dll引用到项目里 然后在根元素的起始 ...
- Elasticsearch常用插件(三)
elasticsearch-head 一个elasticsearch的集群管理工具,它是完全由html5编写的独立网页程序,你可以通过插件把它集成到es. 项目地址:https://github.co ...
- Vimperator技巧
Vimperator技巧 什么是Vimperator?Firefox的一个插件,模拟vim操作. 1. 用]]打开"下一页"链接,[[打开"上一页"Vimper ...
- javabean+servlet+jsp实现分页
前端实现用ligerUI实现分页,感觉用框架确实简单,闲着无聊,模拟着liger的分页界面实现了一遍(只要是功能,样式什么无视) 这里用基础的三层架构+servlet+jsp实现,思路很简单,把所有分 ...
- swift 2 选择头像图片
一句话选择单个头像图片 新建ImagePickerViewController类: /* let imagePicker = ImagePickerViewController() imagePick ...
- 1张图看懂RAID功能,6张图教会配置服务器【转】
RAID 包含一组或者一个集合甚至一个阵列.使用一组磁盘结合驱动器组成 RAID 阵列或 RAID 集.将至少两个磁盘连接到一个 RAID 控制器,而成为一个逻辑卷,也可以将多个驱动器放在一个组中.一 ...
- AC日记——codevs1688求逆序对
AC日记--codevs1688求逆序对 锵炬 掭约芴巷 枷锤霍蚣 蟠道初盛 到被他尽情地踩在脚下蹂躏心中就无比的兴奋他是怎么都 ㄥ|囿楣 定要将他剁成肉泥.挫骨扬灰跟随着戴爷这么多年刁梅生 圃鳋 ...
- 找轮转后的有序数组中第K小的数
我们可以通过二分查找法,在log(n)的时间内找到最小数的在数组中的位置,然后通过偏移来快速定位任意第K个数. 此处假设数组中没有相同的数,原排列顺序是递增排列. 在轮转后的有序数组中查找最小数的算法 ...