LWM
GlobalException.java
package com.bank.util;


import com.bank.exception.ContentEmpyException;
import com.bank.po.ResponseBean;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

//全局异常处理类
@RestControllerAdvice
public class GlobalException {

    //value=某种异常类型的字节码,报那种异常就会执行这个方法
    //相当于catch作用
    @ExceptionHandler(value = Exception.class)
    //响应状态
    @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
    public ResponseBean dealArithException() {
        ResponseBean rb = new ResponseBean(500, "异常");
        return rb;
    }

    //自定义异常
    @ExceptionHandler(value = ContentEmpyException.class)
    //响应状态
    @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
    public ResponseBean ContentEmpyException() {
        ResponseBean rb = new ResponseBean(600, "自定义异常");
        return rb;
    }
}

ContentEmpyException.java

package com.bank.exception;

public class ContentEmpyException extends Exception{

    //自定义异常构造器
    //msg代表业务异常信息
    public ContentEmpyException(String msg){
        super(msg);
    }
}

测试

 

 

 

 








posted on 2023-02-22 21:21  Lwmm  阅读(69)  评论(0编辑  收藏  举报