简介
springmvc对json的前后台传输做了很好封装,避免了重复编码的过程,下面来看看常用的@ResponseBody和@RequestBody注解
添加依赖
springmvc对json的处理依赖jackson
?
1
2
3
4
5
6
7
8
9
10
|
< dependency >
< groupId >org.codehaus.jackson</ groupId >
< artifactId >jackson-core-asl</ artifactId >
< version >1.9.11</ version >
</ dependency >
< dependency >
< groupId >org.codehaus.jackson</ groupId >
< artifactId >jackson-mapper-asl</ artifactId >
< version >1.9.11</ version >
</ dependency >
|
xml配置
?
1
|
< mvc:annotation-driven />//不要忘了命名空间配置
|
如果传输的是单层json对象,我们后台可以直接用 @RequestParam接收
?
1
2
3
4
5
6
7
8
9
10
11
|
$.ajax({
type : "post" ,
dataType : "json" ,
url : "/testRequestBody" ,
data:{
name: "韦德" ,
age:35
},
success : function (result) {
}
});
|
?
1
2
3
4
5
|
@RequestMapping ( "/testRequestBody" )
public String testRequestBody( @RequestParam Map<String, Object> map) {
System.out.println(map); // {name=韦德, age=35}
return "index" ;
}
|
如果传输的是多层嵌套json对象,这个时候会就会出现数据丢失问题
@ResponseBody很好的解决了这个问题,它会把前台传输过来的json转化为后台对应的对象
?
1
2
3
4
5
6
7
8
9
10
11
12
13
|
$.ajax({
type : "post" ,
dataType : "json" ,
url : "/testRequestBody" ,
contentType: "application/json" ,
data:JSON.stringify({
name: "韦德" ,
win:[2006,2012,2013],
age:35
}),
success : function (result) {
}
});
|
?
1
2
3
4
5
|
@RequestMapping ( "/testRequestBody" )
public String testRequestBody( @RequestBody Map<String, Object> map) {
System.out.println(map); //{name=韦德, win=[2006, 2012, 2013], age=35}
return "index" ;
}
|
需要注意的是前台需要指定contentType为"application/json"
同时要把json对象转化为String,否则后台不能识别
ajax请求返回json格式,往常我们可以这样做
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
private void writeJson(HttpServletResponse response, Object object) {
String json = JSON.toJSONString(object);
response.setCharacterEncoding( "UTF-8" );
response.setContentType( "application/json; charset=utf-8" );
PrintWriter out = null ;
try {
out = response.getWriter();
out.write(json);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (out != null ) {
out.close();
}
}
}
|
这个时候 @ResponseBody就派上用场了,只需要一个注解,全部搞定
?
1
2
3
4
5
6
7
8
|
$.ajax({
type : "post" ,
dataType : "json" ,
url : "/testResponseBody" ,
success : function (result) {
console.info(result);
}
});
|
?
1
2
3
4
5
6
7
8
|
@RequestMapping ( "/testResponseBody" )
@ResponseBody
public Map<String, Object> testRequestBody() {
Map<String, Object> result = new HashMap<String, Object>();
result.put( "name" , "韦德" );
result.put( "age" , 35 );
return result;
}
|
前台console输出
?
1
2
3
4
|
{
"age" : 35,
"name" : "韦德"
}
|
总结
在网上看到很不错的流程图,作为总结吧
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。
原文链接:http://www.cnblogs.com/zhaoguhong/p/6882776.html?utm_source=tuicool&utm_medium=referral
相关文章
猜你喜欢
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
- 64M VPS建站:能否支持高流量网站运行? 2025-06-10
- 64M VPS建站:怎样选择合适的域名和SSL证书? 2025-06-10
- 64M VPS建站:怎样优化以提高网站加载速度? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-25 27
-
2025-05-27 20
-
2025-05-27 53
-
2025-05-25 32
-
2025-05-25 31
热门评论