自定义java返回结果类

自定义java通用结果返回类

  1. HttpResult
package com.azor.utils;

public class HttpResult {
	// 响应码
    private Integer code;

    // 响应体
    private String body;


    public HttpResult() {
        super();
    }

    public HttpResult(Integer code, String body) {
        super();
        this.code = code;
        this.body = body;
    }

    public Integer getCode() {
        return code;
    }

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

    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }

	@Override
	public String toString() {
		return "HttpResult [code=" + code + ", body=" + body + "]";
	}

}

  1. StatusCode
package com.azor.utils;

public interface StatusCode {
	
	//成功
	public static final int OK = 20000; 
	
	//失败
	public static final int ERROR = 20001;
	
	//用户名或密码错误
	public static final int LOGINERROR = 20002; 
	
	//权限不足
	public static final int ACCESSERROR = 20003; 
	
	//远程调用失败
	public static final int REMOTEERROR = 20004; 
	
	//重复操作
	public static final int REPERROR = 20005; 
	
}

3.Result

package com.azor.utils;

public class Result {
	
	private boolean flag;
	private Integer code;
	private String message;
	private Object data;
	
	public Result() {
	}
	
	public Result(boolean flag, Integer code, String message) {
		super();
		this.flag = flag;
		this.code = code;
		this.message = message;
	}
	public Result(boolean flag, Integer code, String message, Object data) {
		super();
		this.flag = flag;
		this.code = code;
		this.message = message;
		this.data = data;
	}
	public boolean isFlag() {
		return flag;
	}
	public void setFlag(boolean flag) {
		this.flag = flag;
	}
	public Integer getCode() {
		return code;
	}
	public void setCode(Integer code) {
		this.code = code;
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	public Object getData() {
		return data;
	}
	public void setData(Object data) {
		this.data = data;
	}
	
	/**
	 * 返回成功消息
	 * @return Result
	 */
	public static Result ok() {
		return new Result(true, StatusCode.OK, "成功");
	}
	
	/**
	 * 返回失败消息
	 * @return Result
	 */
	public static Result error() {
		return new Result(true, StatusCode.ERROR, "失败");
	}
	
	/**
	 * 返回登录失败的消息:用户名或密码错误
	 * @return Result
	 */
	public static Result loginError() {
		return new Result(true, StatusCode.LOGINERROR, "用户名或密码错误");
	}
	
	/**
	 * 返回权限不足
	 * @return Result
	 */
	public static Result accessError() {
		return new Result(true, StatusCode.ACCESSERROR, "权限不足");
	}
	
	/**
	 * 返回远程调用失败
	 * @return Result
	 */
	public static Result remoteError() {
		return new Result(true, StatusCode.REMOTEERROR, "远程调用失败");
	}
	
	/**
	 * 返回重复操作
	 * @return Result
	 */
	public static Result repError() {
		return new Result(true, StatusCode.REPERROR, "重复操作");
	}
}

4.使用

Result result = null;
result = new Result(true, StatusCode.OK, "成功");
result = new Result(false, StatusCode.ERROR, "系统内部错误");

猜你喜欢

转载自blog.csdn.net/qq_40053398/article/details/88168135