SpringBoot获取前台传递对象的方法
SpringBoot获取前台传递对象并不容易,稍有错误就会报错。这里提供一个大致模板方法:
前台代码:
let student = {
id: 1,
name: 'tom',
email: '123@qq.com',
age: 15
};
$('#pojoTest').click(function () {
$.ajax({
url: getContextPath() + `/test/test3`,
type: "post",
data: JSON.stringify(student),
contentType: "application/json;charset=utf-8",
success: function (result) {
console.log(result);
},
});
});
后台代码:
@PostMapping("/test3")
public AjaxResult test3(@RequestBody Student student) {
System.out.println(student);
return AjaxResult.ok("姓名:" + student.getName());
}
注意细节:
- 必须使用post请求方式
- 前台对象转为字符串对象
- 后台使用@RequestBoby注解



