全局异常处理

处理方式一:

public class MyException extends RuntimeException {
    private ErrorCodeEnum errorCode;

    public MyException(ErrorCodeEnum errorCode) {
        this.errorCode = errorCode;
    }

    public ErrorCodeEnum getErrorCode() {
        return errorCode;
    }

    public void setErrorCode(ErrorCodeEnum errorCode) {
        this.errorCode = errorCode;
    }
}

2.异常枚举类

public enum ErrorCodeEnum {
    INCORRECT_PASSWORD(10001, "密码错误!"),
    INUSED_USER(10002, "账号错误!"),
    INCORRECT_MSG(10003, "验证码错误!"),

    ErrorCodeEnum(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    private int code;
    private String msg;

    public int getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }
}

3.全局异常处理类

package com.baizhi.exceptions;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
@RestController
@ControllerAdvice
public class MyExceptionHandler {
    //捕捉到的异常
    @ExceptionHandler(value =MyException.class)
    public Map<String,String> getException(MyException e){
        Map<String, String> map = new HashMap<>();
        map.put("msg",e.getErrorCode().getMsg());
        return map;

    }
}

处理方式二:

import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

@Data
@NoArgsConstructor
@AllArgsConstructor
/**
 * 错误信息实体类
 */
public class ErrorMessage implements Serializable {
    private Integer errorCode;
    private String message;

}

两个自定义异常类

package com.baizhi.exceptions;

public class UsernameAndPasswordException extends RuntimeException{
    public UsernameAndPasswordException(String message){
        super(message);
    }

}
package com.baizhi.exceptions;

public class VerifyCodeException extends RuntimeException {
    public VerifyCodeException(String message) {
        super(message);
    }
}
package com.baizhi.controller;

import com.baizhi.entities.ErrorMessage;
import com.baizhi.exceptions.UsernameAndPasswordException;
import com.baizhi.exceptions.VerifyCodeException;
import org.springframework.web.bind.annotation.*;

/**
 * 全局异常处理
 */
@ControllerAdvice
public class MyExceptionHandler {
    @ResponseBody
    @ExceptionHandler(value = UsernameAndPasswordException.class)
    public ErrorMessage errorhandler(UsernameAndPasswordException e){
        return new ErrorMessage(001,e.getMessage());
    }
    @ResponseBody
    @ExceptionHandler(value = VerifyCodeException.class)
    public ErrorMessage errorhandler(VerifyCodeException e){
        return new ErrorMessage(002,e.getMessage());
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值