• 经常遇到的返回值R到底是什么?


    神秘的返回值R

    R是什么

    在看别人的项目的时候会发现,他们的项目后端返回值是R或者是一些没有听说过的字母或者是单词,这些通常是自己或是公司开发定义的一个类,这类中封装了一些属性和方法,这个类中通常包含的属性有:

    • private Boolean success 表示请求是否成功
    • private Integer code 请求的响应码
    • private String message 返回信息
    • private Map<String, Object> data = new HashMap<String, Object>() 返回的数据
      使用这些属性可以表示出一个请求的状态和请求所想传递的信息。
      习惯将R放在一公共包或者公共的项目中,因为其他项目都大概率需要使用到R,公共属性直接调用即可。

    R的特征

    这个类有一些特殊地方:

    • 这个类里面的属性是私有的,即属性是受到保护的不能随意访问
    • 构造器私有化,为了不让其他类new这个对象,即只能自己new自己
    • 大部分方法是静态方法,为了可以直接使用类名.方法名的方式调用方法
    • 每个方法的返回值都是这个类自己,即为了链式调用。java1.8新特性

    例举一个R

    可以直接使用这个R使用

    package com.lei.commonutils;
    
    import io.swagger.annotations.ApiModelProperty;
    import lombok.Data;
    
    import java.util.HashMap;
    import java.util.Map;
    
    @Data
    public class R {
        @ApiModelProperty(value = "是否成功")
        private Boolean success;
    
        @ApiModelProperty(value = "返回码")
        private Integer code;
    
        @ApiModelProperty(value = "返回消息")
        private String message;
    
        @ApiModelProperty(value = "返回数据")
        private Map<String, Object> data = new HashMap<String, Object>();
    
        private R(){}//构造器私有化,导致其他类不能new这个对象,只能自己new自己
    
        //成功静态方法
        public static R ok(){
            R r = new R();
            r.setSuccess(true);
            r.setCode(ResultCode.SUCCESS);
            r.setMessage("成功");
            return r;
        }
    
        //失败静态方法
        public static R error(){
            R r = new R();
            r.setSuccess(false);
            r.setCode(ResultCode.ERROR);
            r.setMessage("失败");
            return r;
        }
    
        //下面的所有方法都返回的是this,链式调用
        public R success(Boolean success){
            this.setSuccess(success);
            return this;
        }
    
        public R message(String message){
            this.setMessage(message);
            return this;
        }
    
        public R code(Integer code){
            this.setCode(code);
            return this;
        }
    
        public R data(String key, Object value){
            this.data.put(key, value);
            return this;
        }
    
        public R data(Map<String, Object> map){
            this.setData(map);
            return this;
        }
    }
    
    

    R作为返回值的使用

    	@GetMapping("getChapterInfo/{chapterId}")
        public R getChapterInfo(@PathVariable String chapterId){
            EduChapter eduChapter = eduChapterService.getById(chapterId);
            return R.ok().data("chapter",eduChapter);
        }
    
        @PostMapping("updateChapter")
        public R updateChapter(@RequestBody EduChapter eduChapter){
            eduChapterService.updateById(eduChapter);
            return R.ok();
        }
    
        @DeleteMapping("{chapterId}")
        public R deleteChapter(@PathVariable String chapterId){
            boolean b = eduChapterService.deleteChapter(chapterId);//如果b为真,说明不能删除,因为小节数目不为空
            if(b){
                return R.error();
            }else{
                eduChapterService.deleteChapter(chapterId);
                return R.ok();
            }
        }
    
    	@GetMapping("getAllComment/{courseId}/{page}/{limit}")
        public R getAllComment(@PathVariable String courseId,@PathVariable long page,@PathVariable long limit){
            //创建分页对象
            Page<EduComment> commentPage = new Page<>(page,limit);
            //service层进行处理
            Map<String,Object> map = eduCommentService.getCommentList(courseId,commentPage);
            return R.ok().data(map);
        }	
    
    	@PostMapping("updateTeacher")
        public R updateTeacher(@RequestBody EduTeacher eduTeacher){
            boolean save = eduTeacherService.updateById(eduTeacher);
            return save==false?R.error():R.ok();
        }
    
  • 相关阅读:
    codevs1074 食物链
    Zjnu Stadium(加权并查集)
    加权并查集(银河英雄传说,Cube Stacking)
    Candies
    SPFA(热浪)
    trie树模板(统计难题)
    你有多久没有看过星星
    欧拉通路、回路
    exkmp
    Number Sequence (HDU 1711)
  • 原文地址:https://www.cnblogs.com/jinjidelei/p/14508135.html
Copyright © 2020-2023  润新知