如何利用切面全局处理异常?

切面控制器代码:

package com.example.empboxwarehousing.config;


import com.example.empboxwarehousing.common.exception.BusinessException;
import com.example.empboxwarehousing.common.response.WebResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 切面全局处理异常
 *
 * @author chp
 * @date 2022-05-13
 */
@ControllerAdvice
@Slf4j
public class ExceptionConfig {

    /**
     * 处理系统异常
     *
     * @param exception 系统异常
     * @return WebResult 自定义返回集合
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public WebResult exception(Exception exception) {

        WebResult webResult = new WebResult();

        webResult.put("code", "9999");

        webResult.put("message", "系统异常,请联系管理员");

        log.error("系统异常Exception信息:{}", exception);

        return webResult;
    }

    /**
     * 处理自定义异常
     *
     * @param exception BusinessException类型,自定义异常
     * @return WebResult 自定义返回集合
     */
    @ExceptionHandler(BusinessException.class)
    @ResponseBody
    public WebResult businessException(BusinessException exception) {

        WebResult webResult = new WebResult();

        webResult.put("code", exception.getCode() == null ? "9999" : exception.getCode());

        webResult.put("message", exception.getMessage() == null ? "系统异常,请联系管理员" : exception.getMessage());

        log.error("系统异常BusinessException信息:{}", exception);

        return webResult;
    }


    /**
     * 参数校验错误
     *
     * @param e MethodArgumentNotValidException 参数校验异常
     * @return WebResult 自定义返回集合
     */
    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    @ResponseBody
    public WebResult handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
        WebResult webResult = new WebResult();

        List<ObjectError> allErrors = e.getBindingResult().getAllErrors();

        String message = allErrors.stream().map(s -> s.getDefaultMessage()).collect(Collectors.joining(";"));

        webResult.put("code", "9999");

        webResult.put("message", message);

        log.error("系统异常Exception信息:{}", message);

        return webResult;
    }

}

自定义异常

package com.example.empboxwarehousing.common.exception;

import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 自定义异常
 *
 * @author chp
 * @date 2022-05-13
 */
@Data
@NoArgsConstructor
public class BusinessException extends RuntimeException {
    /**
     * 返回异常代码:
     * 0000成功,
     * 9999失败
     */
    private String code;

    /**
     * 返回异常信息
     */
    private String message;

    public BusinessException(String message) {
        super();
        this.code = "9999";
        this.message = message;
    }

    public BusinessException(String code, String message) {
        super();
        this.code = code;
        this.message = message;
    }

}

自定义返回集合

package com.example.empboxwarehousing.common.response;

import lombok.Data;

import java.util.HashMap;

/**
 * @author chp
 * @date 2022-05-13
 */
@Data
public class WebResult extends HashMap<String, Object> {
    /**
     * 返回集合code
     */
    private static String CODE = "code";
    /**
     * 返回集合信息
     */
    private static String MESSAGE = "message";

    public WebResult() {
        super();
        super.put(CODE, "200");
        super.put(MESSAGE, "success");
    }

    /**
     * 成功返回
     */
    public static WebResult success() {
        WebResult result = new WebResult();
        result.put(CODE, "200");
        result.put(MESSAGE, "success");
        return result;
    }

    /**
     * 异常返回
     */
    public static WebResult fail(String msg) {
        WebResult result = new WebResult();
        result.put(CODE, "9999");
        result.put(MESSAGE, msg);
        return result;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

慕枫520

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值