spring mvc @RequestBody接受post json对象
来源:互联网 发布:mac 安装apt get命令 编辑:程序博客网 时间:2024/06/05 16:42
jq ajax通过post json对象
遇到的HTTP 415/400问题错误解决方案:http://www.linuxidc.com/Linux/2014-04/99928.htm
客户端:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<html>
<head>
<meta http-equiv=
"Content-Type"
content=
"text/html; charset=UTF-8"
>
<title>Insert title here</title>
<script type=
"text/javascript"
src=
"<%=basePath%>resources/scripts/jquery-2.0.3.min.js"
></script>
<script type=
"text/javascript"
>
$(
function
(){
$(
"#send"
).click(
function
(){
var
jsondata = $(
"#jsondata"
).val();
var
jsonobj = JSON.parse(jsondata)
var
callback =
function
(data) {
$(
"#result"
).html(JSON.stringify(data))
}
$.postJSON(
'<%=basePath%>api/user/test'
, jsonobj, callback)
});
})
$.postJSON =
function
(url, data, callback) {
return
jQuery.ajax({
'type'
:
'POST'
,
'url'
: url,
'contentType'
:
'application/json'
,
'data'
: JSON.stringify(data),
'dataType'
:
'json'
,
'success'
: callback
});
};
</script>
</head>
<body>
JSON对象
<br>
<textarea id=
"jsondata"
cols=
"60"
rows=
"5"
>
{
"id"
:2,
"gender"
:0,
"mobile"
:
"15072370640"
,
"password"
:
"123456"
,
"accessToken"
:
"fd6bf3dd3cca4b0ca7c9099447994dba"
}
</textarea><br>
<button id=
"send"
>POST</button><br>
<font color=
"red"
id=
"result"
></font>
</body>
</html>
spring mvc 服务端:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
@RequestMapping
(value=
"/test"
,method=RequestMethod.POST)
public
@ResponseBody
Map<String, Object> testPostJson(
@RequestBody
UserForm userForm,
BindingResult bindingResult) {
Map<String, Object> map =
new
HashMap<String, Object>();
if
(bindingResult.hasErrors()) {
map.put(
"errorCode"
,
"40001"
);
map.put(
"errorMsg"
, bindingResult.getFieldError().getDefaultMessage());
}
map.put(
"user"
, userForm);
return
map;
}
测试:
结果
1
{
"user"
:{
"name"
:
null
,
"id"
:2,
"password"
:
"123456"
,
"avatar"
:
null
,
"gender"
:0,
"mobile"
:
"15072370640"
,
"accessToken"
:
"fd6bf3dd3cca4b0ca7c9099447994dba"
}}
注意:
@ResponseBody 导致的中文乱码问题见:http://my.oschina.NET/freegeek/blog/287127
0 0
- spring mvc @RequestBody接受post json对象
- spring mvc @RequestBody接受post json对象
- @RequestBody Spring MVC接受对象数组,并接受其它参数
- spring mvc@requestBody接收json对象字符串
- SpringSide4 @RequestBody接受post json对象 (415 Unsupported media type)
- SpringSide4 @RequestBody接受post json对象 (415 Unsupported media type)
- springMVC用@RequestBody接受post json对象 (415 Unsupported media type)
- SpringSide4 @RequestBody接受post json对象 (415 Unsupported media type)
- SpringMVC @RequestBody接受post json对象 (415 Unsupported media type)
- Spring MVC Json @RequestBody 和@ResponseBody
- spring mvc 接受对象数组
- Maven项目:Spring MVC + Ajax + Json + RequestBody:POST后台服务器接收前端JSON数据并注解到POJO内
- Spring MVC @RequestBody 获取前台json或json数组
- Spring MVC接受JSON格式的数据
- Spring MVC接受JSON格式的数据
- Spring MVC 4.0 json post
- ios 接受 java 服务器 框架 spring mvc 解析json对象 存在中文乱码 已解决!!!!!
- spring mvc 接受多对象的处置
- Android Studio项目Gradle构建实践
- 关键字const的作用
- linux环境下的c++
- 微软研究院Detour开发包之API拦截技术
- poi excel列宽设置setColumnWidth
- spring mvc @RequestBody接受post json对象
- 查看数据库建表语句
- 回溯法和分支界限法解决N后问题
- React 环境搭建(学习笔记)
- spring-data-redis 使用 protobuf进行序列化和反序列
- EnumMap学习
- UICollectionView section的头视图 复用BUG解决
- MIPI-DSI 三种 Video Mode 理解
- 蓝桥杯-算法训练-interval-?