API自定义响应类以及对应状态码

package com.ding.beans;

import com.ding.enums.StatusEnum;

import java.io.Serializable;

/**
 * @author hy.ding
 */
public class ResponseBean implements Serializable {

    private static final long serialVersionUID = 1L;

    private int status;

    private Object data = "";

    private String message;

    public void setStatus(StatusEnum codeEnum) {
        this.status = codeEnum.getCode();
        this.message = codeEnum.getMsg();
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public Object getData() {
        return data;
    }

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

    public String getMessage() {
        return message;
    }

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

    @Override
    public String toString() {
        return "ResponseBean{" +
                "status=" + status +
                ", data=" + data +
                ", message='" + message + '\'' +
                '}';
    }
}
package com.ding.enums;

/**
 * 响应状态
 *
 * 

* 以1xx为前缀的状态码表示失败类型 * 以2xx为前缀的状态码表示成功类型 * 以3xx为前缀的状态码表示授权类型 *

* * @author hy.ding */ public enum StatusEnum { FAIL(100, "失败"), SYSTEM_ERROR(101, "系统错误"), PARAM_ERROR(102, "参数错误"), EXCEED_MIN(103, "超过最小值"), EXCEED_MAX(104, "超过最大值"), EMPTY(105, "为空"), SUCCESS(200, "成功"), CREATED(201, "创建成功"), ACCEPTED(202, "已接收"), NOT_FOUND(203, "未找到"), NO_CONTENT(204, "没有内容"), UNAUTHORIZED(300, "未授权"), AUTHORIZATION_INVALID(301, "授权无效"), ; /** * 响应状态码 */ private final int code; /** * 响应提示 */ private final String msg; StatusEnum(int code, String msg) { this.code = code; this.msg = msg; } public int getCode() { return code; } public String getMsg() { return msg; } }

你可能感兴趣的:(Java)