配置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数据的接受和发送的更多相关文章

  1. 使用jQuery解析JSON数据(由ajax发送请求到php文件处理数据返回json数据,然后解析json写入html中呈现)

    在上一篇的Struts2之ajax初析中,我们得到了comments对象的JSON数据,在本篇中,我们将使用jQuery进行数据解析. 我们先以解析上例中的comments对象的JSON数据为例,然后 ...

  2. Spring MVC Json数据传递

    json是一种常见的传递格式,是一种键值对应的格式.并且数据大小会比较小,方便传递.所以在开发中经常会用到json. 首先看一下json的格式: {key1:value1,key2:value2} 每 ...

  3. Spring返回json数据

    第一种形式:使用注解@ResponseBody @RequestMapping(value = "/admin/jq", method = RequestMethod.GET) @ ...

  4. (转)获取 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 ...

  5. 获取 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 ...

  6. 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 ...

  7. Spring MVC如何进行JSON数据的传输与接受

    本篇文章写给刚接触SpingMVC的同道中人,虽然笔者本身水平也不高,但聊胜于无吧,希望可以给某些人带来帮助 笔者同时再次说明,运行本例时,需注意一些配置文件和网页脚本的路径,因为笔者的文件路径与读者 ...

  8. angular2^ typescript 将 文件和Json数据 合并发送到服务器(2.服务端)

    nodejs 中使用框架 express web框架 multer 文件接受 直接贴代码了,我就不解释了 "use strict"; exports.__esModule = tr ...

  9. Spring使用Jackson处理json数据

    1.搭建SpringMVC+Spring环境 2.配置web.xml.SpringMVC-config.xml <?xml version="1.0" encoding=&q ...

随机推荐

  1. CodeForces 669C Little Artem and Matrix GNU

    模拟. 把操作记录一下,倒着复原回去. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cs ...

  2. CSS样式类型

    CSS样式可以写在哪些地方呢?从CSS 样式代码插入的形式来看基本可以分为以下3种:内联式.嵌入式和外部式三种.这一小节先来讲解内联式 (一)内联式样式 内联式css样式表就是把css代码直接写在现有 ...

  3. PAT乙级1006. 换个格式输出整数 (15)

    让我们用字母B来表示“百”.字母S表示“十”,用“12...n”来表示个位数字n(<10),换个格式来输出任一个不超过3位的正整数.例如234应该被输出为BBSSS1234,因为它有2个“百”. ...

  4. php学习笔记——表单

    13.表单 1)GET vs. POST GET 和 POST 都创建数组(例如,array( key => value, key2 => value2, key3 => value ...

  5. Vim/gvim容易忘记的快捷键

    正常模式==>插入模式 按i 在光标前插入 按I 在行首插入 按a 在光标后插入 按s 删除光标所在的字符再插入 按A 在行末插入 按o 在当前行之下新建行 按O 在当前行之上新建行 按S 删除 ...

  6. Process Explorer(增强任务管理器) V16.05 免费绿色版

    软件名称: Process Explorer(增强任务管理器)软件语言: 中文授权方式: 免费软件运行环境: Win7 / Vista / Win2003 / WinXP 软件大小: 1.2MB图片预 ...

  7. jQuery的animate在火狐浏览器上不支持backgroundPositionX的解决方法

    在网上找的ffSupp.js文件 /** * 自定义backgroundPosition的animate,支持火狐,jQuery1.8以上版本 * @author Meleong * v1.00 */ ...

  8. MySql表操作常用语法

    检查表CHECK TABLE table_name;修复表REPAIR TABLE table_name;优化表OPTIMIZE TABLE table_name;分析表ANALYZE TABLE t ...

  9. 仿简书分享:UIActivityViewController系统原生分享

    接下来介绍UIActivityViewController: 1. 创建要分享的数据内容,加在一个数组 ActivityItems里. NSString *textToShare = @"我 ...

  10. myeclipse6.0安装svn插件

    myeclipse6.0安装svn插件 转载地址:http://www.cnblogs.com/danica/archive/2011/07/12/2104323.html myeclipse6.0安 ...