后端对返回数据进行封装

ApiResult.java

package com.dirk.demo.viewmodel;

/**
 * @Author: Dirk
 * @Description: api
 * @Date: Created at 21:49 2018/4/13
 */

public class ApiResult {


    public ApiResult() {
        this.code = 1;
        this.timestamp = System.currentTimeMillis();
    }

    public ApiResult(Object data) {
        this.code = 1;
        this.data = data;
        this.timestamp = System.currentTimeMillis();
    }

    /**
     * 返回API的状态码
     */
    private int code;

    private long timestamp;

    /**
     * 返回的数据
     */
    private Object data;

    public static ApiResult ok() {
        return new ApiResult();
    }

    public static ApiResult ok(Object data) {
        return new ApiResult(data);
    }

    public int getCode() {
        return code;
    }

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


    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public long getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(long timestamp) {
        this.timestamp = timestamp;
    }
}

ApiError.java

package com.dirk.doorlock.viewmodel;

/**
 * @Author: Dirk
 * @Description: api
 * @Date: Created in 13:10 2018/4/14
 */

public class ApiError extends ApiResult {

    /**
     * 未登陆
     */
    public static int UN_LOGIN = -1;

    /**
     * 不存在的用户名
     */
    public static int NOLoginId = -2;

    /**
     * 密码错误
     */
    public static int ErrorPassword = -3;

    /**
     * 上传失败
     */
    public static int FailUpload = -4;


    private String message;

    public ApiError(String message, int status) {
        super();
        setCode(status);
        this.message = message;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    /**
     * 未登陆
     *
     * @return ApiError
     */
    public static ApiError unLogin() {
        return new ApiError("未登陆", UN_LOGIN);
    }

    /**
     * 不存在的用户名
     *
     * @return ApiError
     */
    public static ApiError loginIdNotFound() {
        return new ApiError("不存在的用户名", NOLoginId);
    }


    /**
     * 密码错误
     *
     * @return ApiError
     */
    public static ApiError errorPassword() {
        return new ApiError("密码错误", ErrorPassword);
    }

    public static ApiError failUpload(){
        return new ApiError("上传失败",FailUpload);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值