1. 程式人生 > 其它 >Linux 常用指令篇1

Linux 常用指令篇1

SpringBoot優雅的全域性異常處理

  • 匯入依賴

    <!--fastjson-->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.2.41</version>
    </dependency>
    
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    
    
  • 自定義資料格式

    後臺返回統一資料格式

    //保證序列化Json的時候,如果是null的物件,key也會消失
    @JsonInclude(JsonInclude.Include.NON_NULL)
    public class ResponseResult<T> implements Serializable{
        private int status;
    
        private String msg;
    
        private  T data;
    
        private ResponseResult(int status)
        {
            this.status = status;
        }
        private ResponseResult(int status, T data)
        {
            this.status = status;
            this.data = data;
        }
        private ResponseResult(int status, String msg, T data)
        {
            this.status = status;
            this.msg = msg;
            this.data = data;
        }
        private ResponseResult(int status, String msg)
        {
            this.status = status;
            this.msg = msg;
        }
    
    
        public int getStatus() {
            return status;
        }
    
        //使之不在json序列化結果當中
        @JsonIgnore
        public Boolean isSuccess()
        {
            return this.status == ResponseCode.SUCCESS.getCode();
        }
    
        public String getMsg() {
            return msg;
        }
    
        public T getData() {
            return data;
        }
    
        public static <T> ResponseResult<T> createBySuccess()
        {
            return  new ResponseResult<T>(ResponseCode.SUCCESS.getCode());
        }
    
        public static <T> ResponseResult<T> createBySuccessMessage(String msg)
        {
            return new ResponseResult<T>(ResponseCode.SUCCESS.getCode(),msg);
        }
    
        public static <T> ResponseResult<T> createBySuccess(T data)
        {
            return new ResponseResult<T>(ResponseCode.SUCCESS.getCode(),data);
        }
    
        public static <T> ResponseResult<T> createBySuccess(String msg, T data)
        {
            return  new ResponseResult<T>(ResponseCode.SUCCESS.getCode(),msg,data);
        }
        /******************errorMsg************************/
    
        public static <T> ResponseResult<T> createByError()
        {
            return  new ResponseResult<T>(ResponseCode.SUCCESS.getCode(),ResponseCode.ERROR.getDesc());
        }
        public static <T> ResponseResult<T> createByErrorMessage(String errorMsg)
        {
            return new ResponseResult<T>(ResponseCode.ERROR.getCode(),errorMsg);
        }
        public static <T> ResponseResult<T> createByErrorMessage(int errorCode, String errorMessage)
        {
            return new ResponseResult<T>(errorCode,errorMessage);
        }
    
    }
    
    
  • 自定義列舉類

    統一定義返回資料的格式中的code和message的值

    public enum  ResponseCode {
      
        NEED_LOGIN(10,"NEED LOGIN"),
        ILLEGAL_ARGUMENT(2,"ILLEGAL ARGUMENT"),
        SUCCESS(200, "成功!"),
        BODY_NOT_MATCH(400,"請求的資料格式不符!"),
        SIGNATURE_NOT_MATCH(401,"請求的數字簽名不匹配!"),
        NOT_FOUND(404, "未找到該資源!"),
        INTERNAL_SERVER_ERROR(500, "伺服器內部錯誤!"),
        SERVER_BUSY(503,"伺服器正忙,請稍後再試!");
    
        private final String desc;
        private final int code;
    
        ResponseCode(int code,String desc){
            this.code = code;
            this.desc = desc;
        }
    
        public String getDesc() {
            return desc;
        }
    
        public int getCode() {
            return code;
        }
    }
    
  • 自定義異常類

    @Data
    public class BizException extends  RuntimeException {
    
        private static final Long serialVersionUID = 1L;
    
        /**
         * 錯誤碼
         */
        private int errorCode;
    
        /**
         * 錯誤資訊
         */
        private String errorMessage;
    
        public BizException(String errorMessage) {
            super(errorMessage);
            this.errorMessage = errorMessage;
        }
    
        public BizException(String errorMessage, Throwable cause) {
            super(errorMessage, cause);
            this.errorMessage = errorMessage;
        }
    
        public BizException(int errorCode, String errorMessage) {
            super(errorMessage);
            this.errorMessage = errorMessage;
            this.errorCode = errorCode;
        }
    
        public BizException(int errorCode, String errorMessage, Throwable cause) {
            super(errorMessage,cause);
            this.errorMessage = errorMessage;
            this.errorCode = errorCode;
        }
    }
    
  • 全域性異常處理類

    @RestControllerAdvice
    @Slf4j
    public class GlobalExceptionHandler {
    
        @ExceptionHandler(BizException.class)
        public ResponseResult handleBizException(BizException e)
        {
           return ResponseResult.createByErrorMessage(e.getErrorCode(),e.getErrorMessage());
        }
    
        /**
         * 處理其他異常
         * @param req
         * @param e
         * @return
         */
        @ExceptionHandler(value =Exception.class)
        @ResponseBody
        public ResponseResult exceptionHandler(HttpServletRequest req, Exception e) {
            log.error("未知異常!原因是:", e);
            return ResponseResult.createByErrorMessage(ResponseCode.ILLEGAL_ARGUMENT.getDesc());
    
        }
    }