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 ...
随机推荐
- NGINX----源码阅读一(main函数)
1.ngx_debug_init(); 初始化debug函数,一般为空. 2.ngx_strerror_init(): 将系统错误码+错误信息,以ngx_str_t数组保存. 3.ngx_get_op ...
- CodeForces 706B Interesting drink
排序,二分. 将$x$数组从小到大排序,每次询问的时候只要二分一下位置就可以了. #pragma comment(linker, "/STACK:1024000000,1024000000& ...
- Java的URL来下载网页源码
import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.IOException; impor ...
- 怎么在ng-repeat生成的元素上操作dom
这个问题其实对初学者来说,都 有这样的经历,用ng-repeat生成的元素用js怎么也获取不到;这个其中原由是:angular拥有自动化渲染DOM的特性,它能帮助我们专注于操作数据,而页面的渲染则由a ...
- LeetCode 385. Mini Parse
Given a nested list of integers represented as a string, implement a parser to deserialize it. Each ...
- Anaconda 安装概要
Anaconda 作为开源项目,集成了Python的大部分第三方包,如:pandas,Numpy,scipy等. 1. 下载地址:https://www.continuum.io/downloads ...
- 找轮转后的有序数组中第K小的数
我们可以通过二分查找法,在log(n)的时间内找到最小数的在数组中的位置,然后通过偏移来快速定位任意第K个数. 此处假设数组中没有相同的数,原排列顺序是递增排列. 在轮转后的有序数组中查找最小数的算法 ...
- 读取和存储文本文件,UTF-8和GB2312通用的函数
'------------------------------------------------- '函数名称:ReadTextFile '作用:利用AdoDb.Stream对象来读取UTF-8格式 ...
- Myeclipse启动错误
问题描述: Errors occurred during the build.Errors running builder 'DeploymentBuilder' on project 'szoa'. ...
- mac版Tomcat安装
挺好安装的,就是网上资料有的错了. 1.下载Tomcat 网址:http://tomcat.apache.org,解压在~/Downloads 目录下,我的版本是apache-tomcat-7.0.7 ...