实现步骤:
一、添加业务码类
public class ApiCodeConstant {
public static final int RESULT_SUCC = 20000; //接口请求成功
public static final int RESULT_WARN = 40000; //接口请求警告
public static final int RESULT_ERROR = 50000; //接口请求错误
public static final int RESULT_LOGIN_FAIL = 50001; //用户名或者密码错误
public static final int RESULT_PERMISSION_EXCEPTION= 50002; //自定义权限异常
public static final int RESULT_TOKEN_FAIL = 50014; //token校验失败
public static final int RESULT_NULLPOINT_EXCEPTION= 50101; //空指针异常
public static final int RESULT_OTHER_EXCEPTION= 50102; //其他异常
public static final int RESULT_ID_GEN_EXCEPTION= 502021; //id派发异常
public static final String MSG_ID_GEN_EXCEPTION= "ID生成异常"; //id派发异常消息
public static final int PARAMETER_VALIDATION_ERROR= 50103; //其他异常
}
二、添加请求结果封装类
public class ApiResult extends HashMap<String, Object> {
private static final long serialVersionUID = 1L;
/** 状态码 */
public static final String CODE_TAG = "code";
/** 返回内容 */
public static final String MSG_TAG = "msg";
/** 数据对象 */
public static final String DATA_TAG = "data";
/**
* 构建空信息对象
*/
public ApiResult(){
}
/**
* 初始化一个新创建的 AjaxResult 对象
*
* @param code 状态码
* @param msg 返回内容
* @param data 数据对象
*/
public ApiResult(int code, String msg, Object data)
{
super.put(CODE_TAG, code);
super.put(MSG_TAG, msg);
if (data != null)
{
super.put(DATA_TAG, data);
}
}
/**
* 返回成功消息
*
* @return 成功消息
*/
public static ApiResult success()
{
return ApiResult.success("操作成功");
}
/**
* 返回成功数据
*
* @return 成功消息
*/
public static ApiResult success(Object data)
{
return ApiResult.success("操作成功", data);
}
/**
* 返回成功消息
*
* @param msg 返回内容
* @return 成功消息
*/
public static ApiResult success(String msg)
{
return ApiResult.success(msg, null);
}
/**
* 返回成功消息
*
* @param msg 返回内容
* @param data 数据对象
* @return 成功消息
*/
public static ApiResult success(String msg, Object data)
{
return new ApiResult(ApiCodeConstant.RESULT_SUCC, msg, data);
}
/**
* 返回警告消息
*
* @param msg 返回内容
* @return 警告消息
*/
public static ApiResult warn(String msg)
{
return ApiResult.warn(msg, null);
}
/**
* 返回警告消息
*
* @param msg 返回内容
* @param data 数据对象
* @return 警告消息
*/
public static ApiResult warn(String msg, Object data)
{
return new ApiResult(ApiCodeConstant.RESULT_WARN, msg, data);
}
/**
* 返回错误消息
*
* @return 错误消息
*/
public static ApiResult error()
{
return ApiResult.error("操作失败");
}
/**
* 返回错误消息
*
* @param msg 返回内容
* @return 错误消息
*/
public static ApiResult error(String msg)
{
return ApiResult.error(msg, null);
}
/**
* 返回错误消息
*
* @param msg 返回内容
* @param data 数据对象
* @return 错误消息
*/
public static ApiResult error(String msg, Object data)
{
return new ApiResult(ApiCodeConstant.RESULT_ERROR, msg, data);
}
/**
* 返回错误消息
*
* @param code 状态码
* @param msg 返回内容
* @return 错误消息
*/
public static ApiResult error(int code, String msg)
{
return new ApiResult(code, msg, null);
}
/**
* 是否为成功消息
*
* @return 结果
*/
public boolean isSuccess()
{
return Objects.equals(ApiCodeConstant.RESULT_SUCC, this.get(CODE_TAG));
}
/**
* 是否为警告消息
*
* @return 结果
*/
public boolean isWarn()
{
return Objects.equals(ApiCodeConstant.RESULT_WARN, this.get(CODE_TAG));
}
/**
* 是否为错误消息
*
* @return 结果
*/
public boolean isError()
{
return Objects.equals(ApiCodeConstant.RESULT_ERROR, this.get(CODE_TAG));
}
/**
* 方便链式调用
*
* @param key 键
* @param value 值
* @return 数据对象
*/
@Override
public ApiResult put(String key, Object value)
{
super.put(key, value);
return this;
}
}