相关博客:Restful规范
服务器代码 创建SpringBoot项目 Maven依赖
org.projectlombok
lombok
1.18.10
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.junit.vintage
junit-vintage-engine
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 {
/**
* 请求响应状态码(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 Result success() {
Result result = new Result(ResultCode.SUCCESS);
return result;
}
/**
* 操作成功
*
* @param data
* @return
*/
public static Result success(T data) {
Result result = success();
result.setData(data);
return result;
}
/**
* 操作成功
*
* @param resultCode
* @param data
* @return
*/
public static Result success(ResultCode resultCode, T data) {
Result result = new Result(resultCode, data);
return result;
}
/**
* 操作失败
*
* @param resultCode
* @return
*/
public static Result fail(ResultCode resultCode) {
Result result = new Result(resultCode);
return result;
}
/**
* 操作失败
*
* @param resultCode
* @param data
* @return
*/
public static Result 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 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
关注
打赏
最近更新
- 深拷贝和浅拷贝的区别(重点)
- 【Vue】走进Vue框架世界
- 【云服务器】项目部署—搭建网站—vue电商后台管理系统
- 【React介绍】 一文带你深入React
- 【React】React组件实例的三大属性之state,props,refs(你学废了吗)
- 【脚手架VueCLI】从零开始,创建一个VUE项目
- 【React】深入理解React组件生命周期----图文详解(含代码)
- 【React】DOM的Diffing算法是什么?以及DOM中key的作用----经典面试题
- 【React】1_使用React脚手架创建项目步骤--------详解(含项目结构说明)
- 【React】2_如何使用react脚手架写一个简单的页面?