您当前的位置: 首页 >  restful

梁云亮

暂无认证

  • 5浏览

    0关注

    1211博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Restful 示例

梁云亮 发布时间:2020-05-24 19:57:16 ,浏览量:5

相关博客: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             
关注
打赏
1665409997
查看更多评论
0.0501s