SpringBoot 全局异常处理

SpringBoot 全局异常处理

  • 步骤
    1 引入异常类
    2 添加处理异常方法
    3 扫描该类
    4 抛出相对应的异常
package com.ericsson.cv.devicemgmt.api.interceptor;

import com.ericsson.cv.devicemgmt.api.entiry.BaseResponse;
import com.ericsson.cv.devicemgmt.api.entiry.ErrorResponse;
import com.ericsson.cv.devicemgmt.api.exception.UnauthorizedException;
import javassist.NotFoundException;
import org.springframework.http.HttpStatus;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.client.HttpServerErrorException;

import javax.naming.ServiceUnavailableException;
import javax.servlet.UnavailableException;
import javax.servlet.http.HttpServletRequest;

/**
 SpringBoot Exception Interceptor
 @author eaninfj
 */
@RestControllerAdvice
public class ExceptionControllerAdvice {

    /**
     * UnauthorizedException
     */
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    @ExceptionHandler(UnauthorizedException.class)
    public ErrorResponse handle401() {
        return new ErrorResponse(401,"Access Defined ");
    }

    /**
     * 404 - Not Found
     */
    @ResponseStatus(HttpStatus.NOT_FOUND)
    @ExceptionHandler(NotFoundException.class)
    public ErrorResponse handle404() {
        return new ErrorResponse(404,"Resource Not Found");
    }

    /**
     * 405 - Method Not Allowed
     */
    @ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public ErrorResponse handle405(HttpRequestMethodNotSupportedException e) {
        return new ErrorResponse(405,"Method Not Allowed");
    }

    /**
     * 415 - Unsupported Media Type
     */
    @ResponseStatus(HttpStatus.UNSUPPORTED_MEDIA_TYPE)
    @ExceptionHandler(HttpMediaTypeNotSupportedException.class)
    public ErrorResponse handle415() {
        return new ErrorResponse<>(415,"Unsupported Media Type");
    }

    /**
     * 500 - INTERNAL SERVER ERROR
     */
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler(HttpServerErrorException.class)
    public ErrorResponse handle500() {
        return new ErrorResponse(500,"INTERNAL SERVER ERROR");
    }

    /**
     * 503 - Internal Server Busy
     */
    @ResponseStatus(HttpStatus.SERVICE_UNAVAILABLE)
    @ExceptionHandler(ServiceUnavailableException.class)
    public ErrorResponse handle503() {
        return new ErrorResponse(503,"Internal Server Busy");
    }


    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public ErrorResponse globalException() {
        return new ErrorResponse(400,"Bad Request");
    }

}


触发异常

throw new ShiroException();

结果
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

annan211

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值