<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>
<mvc:annotation-driven />//不要忘了命名空间配置
$.ajax({
type : "post",
dataType : "json",
url : "/testRequestBody",
data:{
name:"韦德",
age:35
},
success : function(result) {
}
});
@RequestMapping("/testRequestBody")
public String testRequestBody(@RequestParam Map<String, Object> map) {
System.out.println(map);// {name=韦德, age=35}
return "index";
}
$.ajax({
type : "post",
dataType : "json",
url : "/testRequestBody",
contentType:"application/json",
data:JSON.stringify({
name:"韦德",
win:[2006,2012,2013],
age:35
}),
success : function(result) {
}
});
@RequestMapping("/testRequestBody")
public String testRequestBody(@RequestBody Map<String, Object> map) {
System.out.println(map);//{name=韦德, win=[2006, 2012, 2013], age=35}
return "index";
}
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();
}
}
}
$.ajax({
type : "post",
dataType : "json",
url : "/testResponseBody",
success : function(result) {
console.info(result);
}
});
@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;
}
{
"age": 35,
"name": "韦德"
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有