1.编写自己捕获异常的包装类

标注绿色的是自己最近写的包装类

BusinessExceptionNew 类

package com.aostar.trademon.ExceptionHandler;

import lombok.Data;

/**
 * @author jay
 * @Description: 自定义业务异常类
 * @date 2021/6/23 14:58
 */
@Data
public class BusinessExceptionNew extends RuntimeException {
    /**
     * 错误编码
     */
    private String code;
    public BusinessExceptionNew() {
        super();
    }
    public BusinessExceptionNew(String message) {
        super(message);
    }
    public BusinessExceptionNew(String code, String message) {
        super(message);
        this.code = code;
    }
    public BusinessExceptionNew(Throwable cause) {
        super(cause);
    }
    public BusinessExceptionNew(String message, Throwable cause) {
        super(message, cause);
    }
    public BusinessExceptionNew(String message, Throwable cause,
                                boolean enableSuppression, boolean writableStackTrace) {
        super(message, cause, enableSuppression, writableStackTrace);
    }
    public String getCode() {
        return code;
    }
    public void setCode(String code) {
        this.code = code;
    }
    @Override
    public String getMessage() {
        return super.getMessage();
    }
    @Override
    public String toString() {
        return this.code + ":" + this.getMessage();
    }
}

SystemExceptionNew类

package com.aostar.trademon.ExceptionHandler;

import lombok.Data;

/**
 * @author jay
 * @Description: 自定义业务异常类
 * @date 2021/6/23 14:58
 */
@Data
public class SystemExceptionNew extends RuntimeException {
    /**
     * 错误编码
     */
    private String code;

    public SystemExceptionNew() {
        super();
    }

    public SystemExceptionNew(String message) {
        super(message);
    }

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

    public SystemExceptionNew(Throwable cause) {
        super(cause);
    }

    public SystemExceptionNew(String message, Throwable cause) {
        super(message, cause);
    }

    public SystemExceptionNew(String message, Throwable cause,
                              boolean enableSuppression, boolean writableStackTrace) {
        super(message, cause, enableSuppression, writableStackTrace);
    }

    public String getCode() {
        return code;
    }

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

    @Override
    public String getMessage() {
        return super.getMessage();
    }

    @Override
    public String toString() {
        return this.code + ":" + this.getMessage();
    }
}

ExceptionAdvice 类

package com.aostar.trademon.ExceptionHandler;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author jay
 * @Description:
 * @date 2021/6/23 14:58
 */
@RestController
@ControllerAdvice
public class ExceptionAdvice {
    public static Logger logger = LoggerFactory.getLogger(ExceptionAdvice.class);

    @ResponseBody
    @ExceptionHandler(SystemExceptionNew.class)
    public ResultVO handleException(Exception e) {
        ResultVO result = new ResultVO();
        if (e instanceof BusinessException) {
            e = (BusinessException) e;
            result.setCode(((BusinessException) e).getCode());
        }
        result.setMessage("系统异常信息:"+e.getMessage());
        return result;
    }

    @ExceptionHandler(RuntimeException.class)
    @ResponseBody
    public ResultVO handleException(RuntimeException e) {
        ResultVO result = new ResultVO();
        result.setStatus("500");
        result.setMessage("运行异常:"+e.getMessage());
        return result;
    }

    @ExceptionHandler(BusinessExceptionNew.class)
    @ResponseBody
    public ResultVO doBusinessException(Exception e) {
        ResultVO result = new ResultVO();
        result.setStatus("500");
        result.setMessage("业务异常:"+e.getMessage());
        return result;
    }

}

返回前端的实体类

package com.aostar.trademon.ExceptionHandler;

/**
 * @author jayd
 * @Description:
 * @date 2021/6/23 14:58
 */

public class ResultVO  {
    private String Code;
    private String Message;
    private String Status;

    public String getCode() {
        return Code;
    }

    public void setCode(String code) {
        Code = code;
    }

    public String getMessage() {
        return Message;
    }

    public void setMessage(String message) {
        Message = message;
    }

    public String getStatus() {
        return Status;
    }

    public void setStatus(String status) {
        Status = status;
    }




}

2.使用方式

代码中throw new 的时候 如果有异常就会将异常信息以ResultVO返回给前端 service层写

3.前台接收异常信息 并进行展示

重点是几个注解的使用

1.Spring通过@ExceptionHandler来拦截系统运行时抛出的相应异常。其有效作用域是其所处的Controller,即它声明的异常处理方法无法拦截、处理其他Controller类中抛出的异常。

2.在类上使用@ControllerAdvice(控制器增强。该注解可以把其声明的类中使用@ExceptionHandler、@InitBinder、@ModelAttribute注解的方法应用到所有的 @RequestMapping注解的方法)声明一个拦截全局异常的@ExceptionHandler。

先自我介绍一下,小编13年上师交大毕业,曾经在小公司待过,去过华为OPPO等大厂,18年进入阿里,直到现在。深知大多数初中级java工程师,想要升技能,往往是需要自己摸索成长或是报班学习,但对于培训机构动则近万元的学费,着实压力不小。自己不成体系的自学效率很低又漫长,而且容易碰到天花板技术停止不前。因此我收集了一份《java开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦

更多推荐

JAVA后台捕获异常,返回异常信息到前端