何时用put,get,post

@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更新

总结

  1. 使用@PutMapping时,只能使用@RequestBody
  2. 使用接口文档用Body,意味着代码层面要用一个对象去包装数据,要使用json去序列化对象。
  3. 我们在开发中经常会用到 @PostMapping、@GetMapping、@RequestParam、@RequestBody、@PathVariable;
    但是我们得注意使用情况:
    @GetMapping 配合 @RequestParam、@RequestBody、@PathVariable;
    @PostMapping 配合 @RequestBody (使用@RequestParam很有可能debug失效或者获取不到参数数据)
    @RequestParam:对包装类,基本类型,String等通过属性value指定参数名
    @RequestBody:对对象,Map,List等直接使用
    @PathVariable:配合属性value使用,来匹配url中的占位符
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值