@PutMapping
场景:前端传给后端几个参数,要求后端进行相关数据的更新。完成更新后,后端一般会返回前端一个布尔值。
方法一:@RequestBody
使用@PutMapping
时,使用@RequestBody
时,后面要用一个类去包装数据。
/**
* 更新定期积分规则
*/
@PutMapping("/months")
public ResultWrapper updatePointMonth(@Valid @RequestBody PointMonthVO pointMonthVO) {
PointMonthDTO pointMonthDTO = new PointMonthDTO();
BeanUtil.copyProperties(pointMonthVO, pointMonthDTO);
return new ResultWrapper(pointService.updatePointMonth(pointMonthDTO));
}
apifox:在Body的json设置参数
方法二:@RequestParam
@PutMapping("/setProgressStartDate")
private ResultWrapper setProgressStartDate(@RequestParam String progressId, @RequestParam String employeeId, @RequestParam String newStartDate) {
LocalDate newStartDateParsed;
try {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
newStartDateParsed = LocalDate.parse(newStartDate, formatter);
}catch (Exception e) {
throw new TrainException(-2,"时间格式错误!");
}
String msg = trainService.setProgressStartDate(progressId,employeeId,newStartDateParsed);
if (msg != null) {
return new ResultWrapper(1, msg);
}
return new ResultWrapper();
}
@GetMapping
方法一:@RequestParam
/**
* 查询所有记录
*/
@GetMapping("/records/all")
public ResultWrapper getAllUserPointRecord(@RequestParam(required = false) Integer group) {
return new ResultWrapper(pointService.retrieveAllPointRecord(group).stream()
.map(DisplayPointRecordVO::DisplayPointRecordDTO2DisplayPointRecordVO)
.collect(Collectors.toList()));
}
apifox:在Params里设置Query参数
方法二:@PathVariable
请求参数放路径里
/**
* 查询用户某类别的详细信息
*/
@GetMapping("/user/{userId}/{type}")
public ResultWrapper getPointUser(@PathVariable String userId, @PathVariable String type) {
PointUserDTO pointUserDTO = null;
if ("engineering".equals(type)) {
pointUserDTO = pointService.retrievePointUserById(userId, PointTypeEnum.ENGINEERING.toString());
} else if ("competition".equals(type)) {
pointUserDTO = pointService.retrievePointUserById(userId, PointTypeEnum.COMPETITION.toString());
} else if ("research".equals(type)) {
pointUserDTO = pointService.retrievePointUserById(userId, PointTypeEnum.RESEARCH.toString());
} else if ("all".equals(type)) {
pointUserDTO = pointService.retrievePointUserById(userId, PointTypeEnum.ALL.toString());
} else if ("public".equals(type)) {
pointUserDTO = pointService.retrievePointUserById(userId, PointTypeEnum.PUBLIC.toString());
} else if ("support".equals(type)) {
pointUserDTO = pointService.retrievePointUserById(userId, PointTypeEnum.SUPPORT.toString());
} else if ("management".equals(type)) {
pointUserDTO = pointService.retrievePointUserById(userId, PointTypeEnum.MANAGEMENT.toString());
}
if (pointUserDTO == null) {
return new ResultWrapper(-1, "不存在当前用户该类别积分记录");
}
return new ResultWrapper(pointUserDTO);
}
apifox:在Params里设置Path参数
@PostMapping
使用@RequestBody
,且后面要用一个类去包装接收的数据。
/**
* 新增待审核积分
*/
@PostMapping("/records")
public ResultWrapper createPointRecord(@Valid @RequestBody PointRecordCreateVO pointRecordCreateVO) {
PointRecordDTO pointRecordDTO = new PointRecordDTO();
BeanUtil.copyProperties(pointRecordCreateVO, pointRecordDTO);
pointRecordDTO = pointService.createNewPointRecord(pointRecordDTO);
PointRecordRetVO pointRecordRetVO = new PointRecordRetVO();
BeanUtil.copyProperties(pointRecordDTO, pointRecordRetVO);
return new ResultWrapper(pointRecordRetVO);
}
apifox:在Body里设置json参数
put和post区别
PUT请求是向服务器端发送数据的,从而改变信息,该请求就像数据库的update操作一样,用来修改数据的内容,但是不会增加数据的种类等,也就是说无论进行多少次PUT操作,其结果并没有不同。
POST请求同PITT请求类似,都是向服务器端发送数据的,但是该请求会改变数据的种类等资源,就像数据库的insert操作一样,会创建新的内容。几乎目前所有的提交操作都是用POST请求的。
就像前面所讲的一样,既然PUT和POST操作都是向服务器端发送数据的,那么两者有什么区别呢。POST主要作用在一个集合资源之上的 (ur1),而PUT主要作用在一个具体资源之上的(ur1/xxx),通俗一下讲就是,如URL可以在客户端确定,那么可使用PUT,否则用POST。
综上所述,可理解为以下:
POST/ur1
创建
PUT/ur1/xxx
更新
总结
- 使用
@PutMapping
时,只能使用@RequestBody
- 使用接口文档用Body,意味着代码层面要用一个对象去包装数据,要使用json去序列化对象。
- 我们在开发中经常会用到 @PostMapping、@GetMapping、@RequestParam、@RequestBody、@PathVariable;
但是我们得注意使用情况:
@GetMapping 配合 @RequestParam、@RequestBody、@PathVariable;
@PostMapping 配合 @RequestBody (使用@RequestParam很有可能debug失效或者获取不到参数数据)
@RequestParam:对包装类,基本类型,String等通过属性value指定参数名
@RequestBody:对对象,Map,List等直接使用
@PathVariable:配合属性value使用,来匹配url中的占位符