配置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 645D Robot Rapping Results Report

    二分,拓扑排序. 二分答案,然后进行拓扑排序检查,若某次发现存在两个或者两个以上入度为$0$的节点,那么不可行. #pragma comment(linker, "/STACK:102400 ...

  2. Python学习笔记——基础篇【第五周】——random & time & datetime模块

    random模块 随机数 mport random print random.random() print random.randint(1,2) print random.randrange(1,1 ...

  3. 利用Paramiko模块远程连接Linux

    使用Paramiko模块模拟SSH远程连接到服务器,并执行命令.(支持Tab键补全) 1.安装相关模块: 1)安装 Crypto 模块: 下载源码包解压 安装: sudo python setup.p ...

  4. jquery 操作listbox 左右相互选择

    实现左右两个listbox的相互选择功能 代码如下: function ListBox_Move(listfrom, listto) { var size = $j("#" + l ...

  5. gvim窗口根据gnome-terminal位置定位

    gvim启动位置固定的话容易挡到东西,所以写了一段vimscript根据gnome-terminal的位置启动gvim,这样被遮住的概率就一些了. fun! g:get_xterm_pos ()&qu ...

  6. iOS学习之Runtime(二)

    前面已经介绍了Runtime系统的概念.作用.部分技术点和应用场景,这篇将会继续学习Runtime的其他知识. 一.Runtime技术点之类/对象的关联对象 关联对象不是为类/对象添加属性或者成员变量 ...

  7. HDU 2516 取石子游戏 斐波纳契博弈

    斐波纳契博弈: 有一堆个数为n的石子,游戏双方轮流取石子,满足: 1)先手不能在第一次把所有的石子取完: 2)之后每次可以取的石子数介于1到对手刚取的石子数的2倍之间(包含1和对手刚取的石子数的2倍) ...

  8. 我对Map端spill的理解

    一.先看简单理解 对于hadoop的map端配置项"mapreduce.task.io.sort.mb"和"mapreduce.map.sort.spill.percen ...

  9. mac版VMware fusion

    百度网盘链接:链接: https://pan.baidu.com/s/1o8BAsrg 安装教程网上很多的,首先要下载一个window 10或其他版本的iso镜像文件,然后很好安装的.

  10. OC 调用JS 代码 处理HTML5 实战

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; ...