SpringBoot中的异常处理

在项目中一般异常都会进行统一管理,在这里采用@ControllerAdvice和@ExceptionHandler进行处理

一、创建 ErrorVO

package com.springBoot.pakage.model;

import lombok.Data;

@Data
public class ErrorVO {

/** 异常编码 */
private Integer errorCode;

/** 异常信息 */
private String errorMessage;
}

二、创建异常枚举

package com.springBoot.pakage.exception;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.springBoot.pakage.constant.Constants;

/**
 * 异常枚举
 */
public enum ComErrorCode {
    SYSTEM_ERROR                    (3000000, "这是业务异常。"),
    SYSTEM_ERROR_GLOBAL             (3000001, "这是全局异常。"),


    ;
    private Integer code;
    private String  message;

    ComErrorCode(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    public Integer getCode() {
        return code;
    }

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

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public JSON toJSON(){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put(Constants.ERROR_CODE,code);
        jsonObject.put(Constants.ERROR_MESSAGE,message);
        return jsonObject;
    }
}

三、业务异常类

package com.springBoot.pakage.exception;

import com.alibaba.fastjson.JSONObject;
import com.springBoot.pakage.constant.Constants;

/**
 * 业务异常类
 */
public class BaseException extends RuntimeException {

    private final Integer errorCode;
    private final String errorMessage;


    public Integer getErrorCode() {
        return errorCode;
    }

    public String getErrorMessage() {
        return errorMessage;
    }

    public String getBizErrorMessage() {
        return "【业务异常】---" + errorMessage;
    }

    public BaseException() {
        super();
        this.errorCode = null;
        this.errorMessage = null;
    }

    public BaseException(String errorMessage) {
        super(errorMessage);
        this.errorCode = null;
        this.errorMessage = errorMessage;
    }

    public BaseException(Integer errorCode, String errorMessage) {
        super(errorMessage);
        this.errorCode = errorCode;
        this.errorMessage = errorMessage;
    }

    public BaseException(ComErrorCode comErrorCode) {
        super(comErrorCode.getMessage());
        this.errorCode = comErrorCode.getCode();
        this.errorMessage = comErrorCode.getMessage();
    }

    public JSONObject toJSON() {
        JSONObject json = new JSONObject();
        json.put(Constants.ERROR_CODE, errorCode);
        json.put(Constants.ERROR_MESSAGE, errorMessage);
        return json;

    }
}

四、Controller异常处理

package com.springBoot.pakage.exception.handler;


import com.springBoot.pakage.exception.BaseException;
import com.springBoot.pakage.exception.ComErrorCode;
import com.springBoot.pakage.model.ErrorVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@Slf4j
@ControllerAdvice
public class ControllerExceptionHandler {

    @ExceptionHandler(BaseException.class)
    @ResponseBody
    public ResponseEntity<ErrorVO> handlerLocalException(BaseException e){
      log.error("【自定义业务异常】 -- [{}]",e.getErrorMessage(),e);
        ErrorVO errorVO = new ErrorVO();
        errorVO.setErrorCode(e.getErrorCode());
        errorVO.setErrorMessage(e.getErrorMessage());
        return ResponseEntity.badRequest().body(errorVO);
    }

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public ResponseEntity<ErrorVO> handlerException(Exception e){
        log.error("【全局异常处理】--- [{}]",e.getMessage(),e);
        ErrorVO errorVO = new ErrorVO();
        errorVO.setErrorCode(ComErrorCode.SYSTEM_ERROR_GLOBAL.getCode());
        errorVO.setErrorMessage(ComErrorCode.SYSTEM_ERROR_GLOBAL.getMessage());
        return ResponseEntity.badRequest().body(errorVO);
    }



}
/**
* controller接口
*/


package
com.springBoot.pakage.controller; import com.alibaba.fastjson.JSONObject; import com.springBoot.pakage.service.TypicalApplicationService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @Slf4j @RestController public class TypicalApplicationController { @Autowired private TypicalApplicationService typicalApplicationService; @GetMapping("/web/info") public ResponseEntity<JSONObject> getInfo(){ JSONObject jsonObject = new JSONObject(); String info = typicalApplicationService.getInfo(); jsonObject.put("STR",info); return ResponseEntity.ok(jsonObject); } @GetMapping("/web/error") public ResponseEntity getInfoError(){ typicalApplicationService.getInfoError(); return ResponseEntity.ok("处理结束"); } }
/**
** 业务层
**/

package
com.springBoot.pakage.service; import com.springBoot.pakage.exception.BaseException; import com.springBoot.pakage.exception.ComErrorCode; import org.springframework.stereotype.Service; @Service public class TypicalApplicationService { public String getInfo(){ if (1==2){ return "aaa"; }else { throw new BaseException(ComErrorCode.SYSTEM_ERROR); } } public void getInfoError(){ int i = 1/0; } }

                                                                                       

 

posted @ 2020-05-29 15:15  stt101517  阅读(445)  评论(0编辑  收藏  举报