firemail
标题:
spring 接收 post参数的不同方法
[打印本页]
作者:
java
时间:
2017-5-25 17:53
标题:
spring 接收 post参数的不同方法
本帖最后由 java 于 2018-4-8 17:22 编辑
1.form-data形式
客户端 以 form-data形式发送
服务端
a. 以@RequestParam 获取每个字段 application/x-www-form-urlencoded ?? 还是url参数?
public Result<AdGroupResult> add(@
RequestParam
(value = "groupName") String groupName,
@
RequestParam
(value = "adPurposeId") Integer adPurposeId) throws Exception{
AdGroupModel adGroupModel = new AdGroupModel();
adGroupModel.setGroupName(groupName);
adGroupModel.setAdPurposeId(adPurposeId);
...
return ..
}
b. 直接用对象接收,不使用任务注解
@RequestMapping(value = "/add", method = RequestMethod.POST, produces = ApiConstant.MEDIA_TYPE_APPLICATION_JSON)
public Result<AdGroupResult> add(
AdGroupModel adGroupModel)
throws Exception{
return adGroupService.add(adGroupModel);
}
2. raw 形式
客户端以json串格式发送
{
"groupName":"dd",
"adPurposeId" : "1"
}
服务端以 @RequestBody 接收到对象中
@RequestMapping(value = "/addByJson", method = RequestMethod.POST, produces = ApiConstant.MEDIA_TYPE_APPLICATION_JSON)
public Result<AdGroupResult> addJson(
@RequestBody AdGroupModel adGroupModel
) throws Exception{
return adGroupService.add(adGroupModel);
}
欢迎光临 firemail (http://firemail.wang:8088/)
Powered by Discuz! X3