@RequestBody的使用场合

       @RequestBody主要用来接收前端传递给后端的json字符串中的数据的(请求体中的数据的),一般使用在前后端分离的项目中,前端使用ajax类的post请求时,会在请求体中携带json格式的数据,例如:

this.$http.post("http://localhost:8989/vue/user/add",this.user).then(res =>{
	console.log(res);
	});

       其中的this.user就是一个js对象,后端的springMVC会通过jackson对传来的对象解析,并封装到一个类中,这时后端接口参数类前就需要@RequestBody注解。
       有时候不需要@RequestBody注解也可以将前端发来的参数封装到一个类中,例如form表单(jsp页面):

<form action="${pageContext.request.contextPath}/user/register" method="post">
    用户名:<input type="text" name="username"> <br/>
    密码:<input type="text" name="password"> <br/>
    登录:<input type="submit" value="注册"> <br/>
</form>

       这个时候后端接口里的参数前就不需要加@RequestBody注解了,会根据input中的name的值匹配后端接口参数类中的属性来进行封装。

猜你喜欢

转载自blog.csdn.net/weixin_42822484/article/details/106790156