1. 程式人生 > 實用技巧 >Result類,公共返回類建立

Result類,公共返回類建立

package com.deng.hongbao.base;
/**
* 先構建一個格式確定下來的Result類
* 然後構建一個常用的列舉類,內容可以根據專案自定義,檢視ResultEnum
* 然後就是寫一個返回的工具類ResultUtil
* 最後就是應用了,在Controller裡邊呼叫ResultUtil就可以了
*
* 還要考慮拋異常情況,就是當條件滿足時,直接把資訊拋給瀏覽器,比如未滿18歲,返回瀏覽器"未成年"
* 建立一個handle包,建立一個ExceptionHandle類(或者XXServiceException類)
*再建立一myException包,建立一個serviceException類繼承RuntimeException類

* */
public class Result<T> {
private Integer code;
private String msg;
private T data;

public Result() {
super();
}

public Result(Integer code, String msg, T data) {
this.code = code;
this.msg = msg;
this.data = data;
}

public Integer getCode() {
return code;
}

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

public String getMsg() {
return msg;
}

public void setMsg(String msg) {
this.msg = msg;
}

public T getData() {
return data;
}

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

@Override
public String toString() {
return "Result{" +
"code=" + code +
", msg='" + msg + '\'' +
", data=" + data +
'}';
}
}

//------------------------------------

package com.deng.hongbao.base;

public enum ResultEnum {
//這裡是可以自己定義的,方便與前端互動即可
UNKNOWN_ERROR(-1,"未知錯誤"),
SUCCESS(0,"成功"),
USER_NOT_EXIST(1,"使用者不存在"),
USER_IS_EXISTS(2,"使用者已存在"),
DATA_IS_NULL(3,"資料為空"),
DENG_1(999,"傳送人為空1111"),// 測試
;
private Integer code;
private String msg;


ResultEnum(Integer code, String msg) {
this.code = code;
this.msg = msg;
}

public Integer getCode() {
return code;
}

public String getMsg() {
return msg;
}
}

//-------------------------------------
package com.deng.hongbao.util;

import com.deng.hongbao.base.Result;
import com.deng.hongbao.base.ResultEnum;

public class ResultUtil {
/**成功且帶資料**/
public static Result success(Object object){
Result result = new Result();
result.setCode(ResultEnum.SUCCESS.getCode());
result.setMsg(ResultEnum.SUCCESS.getMsg());
result.setData(object);
return result;
}
/**成功但不帶資料**/
public static Result success(){

return success(null);
}
/**失敗**/
public static Result error(Integer code,String msg){
Result result = new Result();
result.setCode(code);
result.setMsg(msg);
return result;
}
}

//----------------------------------------------------------------

package com.deng.hongbao.handle;

import com.deng.hongbao.MyException.ServiceException;
import com.deng.hongbao.base.Result;
import com.deng.hongbao.util.ResultUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class ExceptionHandle {
private final static Logger logger = LoggerFactory.getLogger(ExceptionHandle.class);

@ExceptionHandler(value = Exception.class)//對哪個異常類進行捕獲
@ResponseBody
public Result handle(Exception e) {

if (e instanceof ServiceException) {
ServiceException serviceException = (ServiceException) e;
return ResultUtil.error(serviceException.getCode(), serviceException.getMessage());
} else {
// 如果是系統異常或其它異常,記錄在日誌
logger.error("[系統異常] {}", e);
return ResultUtil.error(-1, "未知錯誤!");
}
}

// 成功用ResultUtil丟擲異常,但問題來了,這個異常類Exception,不能自定義ResultUtil的code,所以要自定義一個Exception
}

// ----------------------------------------------------------

package com.deng.hongbao.MyException;

import com.deng.hongbao.base.ResultEnum;

public class ServiceException extends RuntimeException {

// 繼承RuntimeException異常可以事物回滾,Exception只負責丟擲,但不回滾事物

private Integer code;

public ServiceException(Integer code, String msg) {
super(msg);// 由於RuntimeException繼承了Exception,本身會有個msg檔案,所以直接呼叫
this.code = code;
}

public ServiceException(ResultEnum resultEnum) {
super(resultEnum.getMsg());
this.code = resultEnum.getCode();
}

public Integer getCode() {
return code;
}

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