Global SpringMVC exception handling

@ControllerAdvice use

We all know to do the project in general will have a unified global exception handling class, then the class can be achieved with @ControllerAdvice in Spring.
@ControllerAdvice, this is a very useful comments, as the name suggests, this is an enhancement of the Controller. Use the Controller, three functions can be achieved:

Global exception handler
global data binding
global data preprocessing
flexible use of these three functions that can help us to simplify a lot of work needs to be noted that this is SpringMVC provide features that can be used directly in Spring Boot in.

package com.boss.hr.train.fishkkmybatis.handle;

import com.boss.hr.train.fishkkmybatis.entity.Result;
import com.boss.hr.train.fishkkmybatis.enums.DictionaryEnum;
import com.boss.hr.train.fishkkmybatis.exception.BaseException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindException;
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 java.util.ArrayList;
import java.util.List;

/**
 * ExceptionHandel
 * 全局异常处理类
 *
 * @author fishkk
 * @version 1.0
 * @since 2019/7/27
 */
@ControllerAdvice
public class ExceptionHandle {
    private final static Logger logger = LoggerFactory.getLogger(ExceptionHandle.class);

    /**
      * 全局异常处理
      * 自定义异常  表单验证异常 和 未定义系统异常的处理
      * 转换成Result对象返回
      * @author fishkk
      * @since  2019/7/28
      * @param  e 被捕获的异常
      * @return  Result
      */
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Result handle(Exception e){
        if (e instanceof BaseException) {
            // 系统内部异常
            BaseException exception = (BaseException) e;
            return new Result<Object>(exception.getResultEnum(), null);
        }
        if(e instanceof BindException){
            // @Valid表单验证不通过
            BindException bindException = (BindException)e;
            List<ObjectError> errors = bindException.getAllErrors();
            List<String> errorMessages = new ArrayList<>();
            for (ObjectError objectError : errors){
                errorMessages.add(objectError.getDefaultMessage());
            }

            return Result.error("-300", e.getMessage());
        } else {
            logger.error("!!!系统异常!!!", e);
            return new Result<Object>(DictionaryEnum.UNKNOW_ERROR, null);
        }
    }
}

@ExceptionHandler annotation process used to indicate the type of anomaly, we are here to capture all exceptions. Then the corresponding processing and encapsulation of the abnormality determination of the type, return custom respective classes and enumerating configured with custom exception class,
this can be changed from the line depending on the actual situation

Guess you like

Origin www.cnblogs.com/fishkk/p/11371966.html