服务器代码

创建SpringBoot项目

Maven依赖

    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.10</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

application.yml

server:
  port: 80
  servlet:
    context-path: /demo

返回统一格式数据相关

ResultCode.java

public enum ResultCode {
    /* 成功状态码 */
    SUCCESS(200, "操作成功"),

    /**
     * 客户端错误
     */
    CLIENT_FAIL(400, "客户端错误"),

    /**
     * 服务器端错误
     */
    SERVER_FAIL(500, "服务器端错误")

    ;

    /**
     * 操作代码
     */
    int code;
    /**
     * 提示信息
     */
    String msg;

    ResultCode(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }
}

Result.java

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Result<T> {
    /**
     * 请求响应状态码(200、400、500)
     */
    private int code;
    /**
     * 请求结果描述信息
     */
    private String msg;
    /**
     * 请求结果数据
     */
    private T data;

    private Result(ResultCode resultCode) {
        if (resultCode == null) {
            return;
        }
        this.code = resultCode.code();
        this.msg = resultCode.msg();
    }

    private Result(ResultCode resultCode, T data) {
        this(resultCode);
        this.data = data;
    }

    /**
     * 操作成功
     *
     * @return
     */
    public static <T> Result<T> success() {
        Result result = new Result(ResultCode.SUCCESS);
        return result;
    }

    /**
     * 操作成功
     *
     * @param data
     * @return
     */
    public static <T> Result<T> success(T data) {
        Result result = success();
        result.setData(data);
        return result;
    }

    /**
     * 操作成功
     *
     * @param resultCode
     * @param data
     * @return
     */
    public static <T> Result<T> success(ResultCode resultCode, T data) {
        Result result = new Result(resultCode, data);
        return result;
    }

    /**
     * 操作失败
     *
     * @param resultCode
     * @return
     */
    public static <T> Result<T> fail(ResultCode resultCode) {
        Result result = new Result(resultCode);
        return result;
    }

    /**
     * 操作失败
     *
     * @param resultCode
     * @param data
     * @return
     */
    public static <T> Result<T> fail(ResultCode resultCode, T data) {
        Result result = new Result(resultCode, data);
        return result;
    }
}

实体类

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
@ToString
public class Dept {
    private Integer deptno;
    private String dname;
    private String loc;
}

数据库模拟类

public class DeptTable {
    private static ArrayList<Dept> depts = new ArrayList<>();

    static {
        depts.add(new Dept(10, "ACCOUNTING", "CHICAGO"));
        depts.add(new Dept(20, "RESEARCH", "DALLAS"));
        depts.add(new Dept(30, "SALES", "CHICAGO"));
        depts.add(new Dept(40, "OPERATIONS", "BOSTON"));
    }

    public static boolean insert(Dept dept) {
        boolean res = depts.add(dept);
        return res;
    }

    public static boolean update(Dept dept) {
        Integer deptno = dept.getDeptno();
        boolean flag = false;
        for (int i = 0; i < depts.size(); i++) {
            Dept temp = depts.get(i);
            if (temp.getDeptno().equals(deptno)) {
                depts.set(i, dept);
                flag = true;
            }
        }
        return flag;
    }

    public static boolean delete(Integer deptno) {
        boolean flag = false;
        for (int i = 0; i < depts.size(); i++) {
            Dept dept = depts.get(i);
            if (dept.getDeptno().equals(deptno)) {
                depts.remove(i);
                flag = true;
            }
        }
        return flag;
    }

    public static Dept select(Integer deptno) {
        for (int i = 0; i < depts.size(); i++) {
            Dept dept = depts.get(i);
            if (dept.getDeptno().equals(deptno)) {
                return dept;
            }
        }
        return null;
    }

    public static void output() {  //输出所有数据,查看测试结果用
        for (Dept dept : depts) {
            System.out.println(dept);
        }
    }
}

Controller

@Slf4j
@RestController
public class DeptController {

    //获取Dept,使用GET方法
    @GetMapping(value = "/v2/depts/{deptno}")
    public Result getDeptByDeptno(@PathVariable("deptno") Integer deptno) {
        Dept dept = DeptTable.select(deptno);
        if (dept != null) {
            return Result.success(dept);
        } else {
            return Result.success(ResultCode.SERVER_FAIL);
        }
    }

    //获取所有Dept
    @GetMapping("/v2/depts")
    public Result<List<Dept>> getAllDept() {
        List<Dept> depts = DeptTable.selectAll();
        return Result.success(depts);
    }

    //增加Dept ,使用POST方法
    @PostMapping(value = "/v1/depts")
    public Result saveDept(@RequestBody Dept dept) {
        boolean res = DeptTable.insert(dept);
        log.info("saveDept:{}", dept);
        DeptTable.selectAll().forEach(System.out::println);
        if (res) {
            return Result.success(dept);
        } else {
            return Result.fail(ResultCode.SERVER_FAIL);
        }
    }

    //更新Dept,使用PUT方法
    @PutMapping(value = "/v1/depts/{deptno}")
    public Result updateDept(@PathVariable("deptno")Integer deptno,@RequestBody Dept dept) {
        dept.setDeptno(deptno);
        boolean flag = DeptTable.update(dept);//表示用户要删除的deptno不存在
        log.info("updateDept:{}", dept);
        DeptTable.selectAll().forEach(System.out::println);
        if (flag) {
            return Result.success(dept);
        } else {
            return Result.fail(ResultCode.SERVER_FAIL);
        }
    }

    //删除Dept,使用DELETE方法
    @DeleteMapping(value = "/v1/depts/{deptno}")
    public Result deleteDept(@PathVariable Integer deptno) {
        boolean flag = DeptTable.delete(deptno);
        log.info("deleteDept:{}", deptno);
        DeptTable.selectAll().forEach(System.out::println);
        if (flag) {
            return Result.success(deptno);
        } else {
            return Result.fail(ResultCode.CLIENT_FAIL);
        }
    }

}
API说明:
请求方式 api 功能
Get /v2/depts/{deptno} 获取指定编号的部门
Get /v2/depts 获取所有的部门
post /v1/depts 增加部门
put /v1/depts/{deptno} 修改部门
delete /v1/depts/{deptno} 删除指定的部门

测试部分

启动项目,采用Restful toolkit进行测试
在这里插入图片描述

/v2/depts/{deptno}

在这里插入图片描述

/v2/depts

在这里插入图片描述

/v1/depts

在这里插入图片描述

/v1/depts/{deptno}

在这里插入图片描述
结果:
在这里插入图片描述

/v1/depts/deptno

在这里插入图片描述