@RequestBody 和 @RequestParam
1.@RequestParam(可以有多个)
可以接收单个参数传递(必须有对应的名字)
@ApiOperation("transmit")
@PostMapping("/transmit")
public Result transmit(@RequestParam(required = false) @ApiParam(value = "id")String id
,@ApiParam(value = "用户id") @RequestParam(required = true) Long userId) {
//根据userId查询用户
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date = format.format(new Date());
User user = userService.findById(userId);
if(StringUtils.isEmpty(id)){
return ResultGenerator.genFailResult("id不能为空");
}
return ResultGenerator.genSuccessResult(1);
}
2.@RequestBody (一次请求只能有一个)
接收对象类型参数
前端参数需要放在Body中,不然接收不到
@ApiOperation("transmit")
@PostMapping("/transmit")
public Result transmit(@Valid @RequestBody Transmit transmit) {
//根据userId查询用户
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date = format.format(new Date());
return ResultGenerator.genSuccessResult(1);
}
前端参数放在body中区别对比图