說明:僅且返回狀態爲0時說明正確返回,其餘都是異常或錯誤返回
1、封裝返回數據狀態,數據,信息Result .java
@ApiModel(description = "接口返回響應數據")
public class Result extends HashMap<String,Object> implements java.io.Serializable {
public Result setSuccess(Object data) {
return this.setSuccess("操作成功", data);
}
public Result setSuccess(String msg) {
return this.setSuccess(msg, null);
}
public Result setSuccess(String msg, Object data) {
this.put("msg", msg);
this.put("data", data);
this.put("status", BaseResponseCode.SUCCESS.getStatus());
return this;
}
public Result setError(String msg) {
return this.setError(msg, BaseResponseCode.ERROR.getStatus());
}
public Result setError(Integer status) {
return this.setError("操作失敗", status);
}
public Result setError(String msg, Integer status) {
this.put("msg", msg);
this.put("status", status);
return this;
}
public Result setError(ResponseCodeInterface code) {
this.put("msg", code.getMsg());
this.put("status", code.getStatus());
return this;
}
public Result setResult(String msg, Object data, Integer status) {
this.put("msg", msg);
this.put("data", data);
this.put("status", status);
return this;
}
}
常量
2、封裝異常返回數據
自定義異常接口
ResponseCodeInterface.java
public interface ResponseCodeInterface {
int getCode();
String getMsg();
}
枚舉類:BaseResponseCode.java
public enum BaseResponseCode implements ResponseCodeInterface {
SUCCESS(0, "操作成功"),
ERROR(1, "操作異常"),
/**
* 賬戶異常
*/
USER_EXIST(400001, "該用戶已存在,請重新註冊"),
USER_EXIST1(400001, "該郵箱存在,請重新註冊"),
NOT_ACCOUNT(400002, "該用戶不存在,請先註冊"),
USER_LOCK(400005, "該用戶已被鎖定,請聯繫運營人員"),
ACCOUNT_ERROR(400009, "該賬號異常,請聯繫運營人員"),
PASSWORD_ERROR(400010,"用戶名或密碼錯誤"),
OLD_PASSWORD_ERROR(400011,"舊密碼不正確"),
USER_PASSWORD_ERROR(400012,"用戶或密碼不正確"),
/**
* 請求參數異常
*/
DATA_ERROR(400020,"傳入數據異常"),
/**
* 前端操作異常
*/
OPERATION_MENU_PERMISSION_CATALOG_ERROR(401031,"操作後的類型是目錄,所屬菜單必須爲默認頂級菜單或者目錄"),
OPERATION_MENU_PERMISSION_MENU_ERROR(401032,"操作後的類型是菜單,所屬菜單必須爲目錄類型"),
OPERATION_MENU_PERMISSION_BTN_ERROR(401033,"操作後的類型是按鈕,所屬菜單必須爲菜單類型"),
/**
* token異常
*/
TOKEN_ERROR(401001, "登錄憑證已過期,請重新登錄"),
TOKEN_PAST_DUE(401002, "授權信息已過期,請刷新token"),
/**
* 權限異常
*/
UNAUTHORIZED_ERROR(403001, "權鑑校驗不通過"),
/**
* 操作數據庫異常
*/
REGISTER_ERROR(500001, "註冊失敗,請嘗試重新註冊"),
;
private final String msg;
private final int status;
BaseResponseCode(int status, String msg) {
this.status = status;
this.msg = msg;
}
@Override
public int getStatus() {
return status;
}
@Override
public String getMsg() {
return msg;
}
}
異常類:BusinessException.java
public class BusinessException extends RuntimeException {
/**
* 異常編號
*/
private final int messageCode;
/**
* 對messageCode 異常信息進行補充說明
*/
private final String detailMessage;
public BusinessException(int messageCode,String message) {
super(message);
this.messageCode = messageCode;
this.detailMessage = message;
}
/**
* 構造函數
* @param code 異常碼
*/
public BusinessException(ResponseCodeInterface code) {
this(code.getCode(), code.getMsg());
}
public int getMessageCode() {
return messageCode;
}
public String getDetailMessage() {
return detailMessage;
}
}
3、正常返回與異常返回
controller層 LoginRegisterController.java將調用下面service層方法
public Result register(RegisterReqVO vo){
Result result= new Result();
try {
trUserService.register(vo);
result.setSuccess("註冊成功");
} catch (BusinessException e) {
e.printStackTrace();
result.setError(e.getMessageCode(),e.getDetailMessage());
} catch (Exception e) {
e.printStackTrace();
result.setError("註冊異常");
return result;
}
return result;
}
service層:TRUserServiceImpl.java
@Override
public String register(RegisterReqVO vo) throws Exception {
// 先檢查數據庫是否有在用的賬號或郵箱,只要就一個就不註冊
if (BeanUtil.isNotEmpty(vo.getUsername())) {
if (findByUsername(vo.getUsername())) {
throw new BusinessException(BaseResponseCode.USER_EXIST);
}
}
if (BeanUtil.isNotEmpty(vo.getEmail())) {
if (findByEmail(vo.getEmail())) {
throw new BusinessException(BaseResponseCode.USER_EXIST1);
}
}
TRUser trUser = new TRUser();
BeanUtils.copyProperties(vo, trUser);
trUser.setSalt(PasswordUtils.getSalt());
String encode = PasswordUtils.encode(vo.getPassword(), trUser.getSalt());
trUser.setPassword(encode);
trUser.setCreatDate(new Date());
save(trUser);
return trUser.getId();
}
說明:當正常返回或者走到Exception異常時返回Result,當走自定義異常BusinessException時,返回BusinessException中錯誤狀態碼與錯誤說明