1. 程式人生 > 實用技巧 >全域性捕獲異常

全域性捕獲異常

首先需要一個公共的返回響應類

package com.wd.common.response;

import com.wd.common.exception.CodeMsg;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;

@ApiModel(value = "返回響應類")
public class ResponseModel<T extends Object> implements Serializable {

    @ApiModelProperty(value 
= "響應碼") private Integer code; @ApiModelProperty(value = "響應訊息") private String msg; @ApiModelProperty(value = "響應資料") private T data; public static <T> ResponseModel<T> success(T data){ return new ResponseModel<T>(data); } public static <T> ResponseModel<T> error(CodeMsg cm){
return new ResponseModel<T>(cm); } private ResponseModel(T data) { this.code=0; this.msg="success"; this.data=data; } private ResponseModel(CodeMsg cm) { if(cm==null){ return; } this.code=cm.getCode(); this.msg=cm.getMsg(); }
public ResponseModel(String msg, T data) { this.code = 0; this.msg = msg; this.data = data; } public ResponseModel(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; } }
package com.wd.common.exception;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

@ApiModel(value = "異常錯誤碼", description = "")
public class CodeMsg {
    @ApiModelProperty(value = "響應碼")
    private Integer code;
    @ApiModelProperty(value = "響應訊息")
    private String msg;

    //通用的錯誤碼
    public static CodeMsg SERVER_ERROR = new CodeMsg(100001, "服務端異常,程式設計師小哥正在維護中!");
    public static CodeMsg BIND_ERROR = new CodeMsg(100002, "引數校驗異常!");
    public static CodeMsg NOT_PARAMETER = new CodeMsg(100002, "缺少關鍵引數欄位!");
    public static CodeMsg NOT_NULL = new CodeMsg(100002, "未查詢到資料!");





    public CodeMsg() {
    }

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

    public CodeMsg fillArgs(Object... args) {
        int code = this.code;
        String message = String.format(this.msg, args);
        return new CodeMsg(code, message);
    }

    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;
    }
}
package com.wd.common.exception;

public class GlobalException extends RuntimeException{

    private static final long serialVersionUID = 1L;

    private CodeMsg codeMsg;

    public GlobalException(CodeMsg codeMsg) {
        super(codeMsg.toString());
        this.codeMsg = codeMsg;
    }

    public CodeMsg getCm() {
        return codeMsg;
    }
}
package com.wd.common.exception;

import com.wd.common.response.ResponseModel;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.net.BindException;
import java.util.List;

@ControllerAdvice //該註解定義全域性異常處理類
//@ControllerAdvice(basePackages ="com.wd.business.service") // 指定包
@ResponseBody
public class GlobalExceptionHandler {
    @ExceptionHandler(value=Exception.class) //該註解宣告異常處理方法
    public ResponseModel<String> exceptionHandler(HttpServletRequest request, Exception e){
        e.printStackTrace();
        if (e instanceof GlobalException){
            GlobalException ex = (GlobalException) e;
            return ResponseModel.error(ex.getCm());
        }else if (e instanceof BindException){
            //對於繫結異常的處理,使用jsr303中的自定義註解丟擲的異常屬於繫結異常
            BindException ex = (BindException) e;
            String message = ex.getMessage();
            return ResponseModel.error(CodeMsg.BIND_ERROR.fillArgs(message));
        }else {
            return ResponseModel.error(CodeMsg.SERVER_ERROR);
        }

    }
}