在上代码之前,有必要先说说@ResquestBody注解的含义:
1、官方解释如下:
Annotation indicating a method parameter
should be bound to the body of the web request.
The body of the request is passed through an {@link HttpMessageConverter}
to resolve the method argument depending on the content type of the request.
(意思大概是:用该注解标识的方法的参数,会和web请求体绑定。
http消息转换器会根据content-type的设置将请求体解析,从而初始化该方法的参数。)
2、另外还需解释一下使用的场景
GET、POST方式提交的请求:
Content-type:
1、application/x-www-form-urlencoded:@RequestBody不是必须加的
2、mutipart/form-data:@RequestBody不能处理这种格式
3、其他格式,比如application/json,application/xml等,必须使用@RequestBody来处理
PUT方式提交的请求:
以上1和3的场景都是必须使用@RequestBody来处理的,2场景也是不支持的
3、前端代码如下:(这里必须将JSON对象使用JSON.stringify()转为JSON字符串再传递,否则后台接收不到值)
$.ajax({
url:"../../Notice/LoadForm.do",
type:"post",
contentType:"application/json;charset=UTF-8",
data:JSON.stringify({"id":"1","title":"标题"})
});
4、后台接收代码示例
@RequestMapping(value="Notice/LoadForm")
@ResponseBody
public ResultJO loadForm(@RequestBody Notice notice){
}
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有