栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Springboot 自定义全局异常处理

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Springboot 自定义全局异常处理

在项目根包目录下新建 exception.base 包
新建BaseException 继承 RuntimeException

package com.ddz.errordemo.handler;


public class BaseException extends RuntimeException {
    
    private String module;

    
    private String code;

    
    private Object[] args;

    
    private String defaultMessage;

    public BaseException(String module, String code, Object[] args, String defaultMessage) {
        this.module = module;
        this.code = code;
        this.args = args;
        this.defaultMessage = defaultMessage;
    }

    public BaseException(String module, String code, Object[] args) {
        this(module, code, args, null);
    }

    public BaseException(String module, String defaultMessage) {
        this(module, null, null, defaultMessage);
    }

    public BaseException(String code, Object[] args) {
        this(null, code, args, null);
    }

    public BaseException(String defaultMessage) {
        this(null, null, null, defaultMessage);
    }

    public String getModule() {
        return module;
    }

    public void setModule(String module) {
        this.module = module;
    }

    public String getCode() {
        return code;
    }

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

    public Object[] getArgs() {
        return args;
    }

    public void setArgs(Object[] args) {
        this.args = args;
    }

    public String getDefaultMessage() {
        return defaultMessage;
    }

    public void setDefaultMessage(String defaultMessage) {
        this.defaultMessage = defaultMessage;
    }
}

根据自己的需要新建业务异常类

package com.ddz.errordemo.exception;

import com.ddz.errordemo.exception.base.BaseException;


public class UserException extends BaseException
{
    private static final long serialVersionUID = 1L;

    public UserException(String code, Object[] args)
    {
        super("user", code, args, null);
    }
}

在项目根包目录下新建 exception.handler包;
新建RestExceptionHandler 异常处理类

package com.ddz.errordemo.exception.handler;

import com.ddz.errordemo.exception.base.BaseException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;


@RestControllerAdvice
public class RestExceptionHandler {
    @ExceptionHandler(Exception.class)
    public Object exception(HttpServletRequest request, Exception e) {
        return request.getRequestURL() + "全局异常" + e.getMessage();
    }

    @ExceptionHandler(NullPointerException.class)
    public Object nullException(Exception e) {
        return "空指针" + e.getMessage();
    }

    @ExceptionHandler(RuntimeException.class)
    public Object busException(BaseException e) {
        return "模块" + e.getModule() + "异常码:" + e.getCode() + "异常信息:" + e.getMessage();
    }
}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/837966.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号