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

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

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

BusinessExceptionNew 类

package com.aostar.trade.common.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.trade.common.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.trade.common.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.trade.common.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。
 

  • 3
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值