springboot全局异常配置

本文档介绍了如何使用Spring Boot和Shiro实现全局异常处理,包括权限异常、参数缺失、token验证失败和格式化异常的处理,以及通用异常的统一响应。
package com.tongsheng.common.advice;

import com.tongsheng.common.constant.Result;
import com.tongsheng.common.constant.ResultInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author Yw
 * @version 1.0
 * @date 2019-12-25
 * @description 全局异常处理
 */
@Slf4j
@RestControllerAdvice
public class ExceptionAdvice {

    /**
     * shiro权限异常处理
     *
     * @return result
     */
    @ExceptionHandler(AuthorizationException.class)
    public Result handleAuthorizationException() {
        return Result.getResult(ResultInfo.FORBIDDEN_ERROR);
    }

    /**
     * 处理参数异常
     *
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value = IllegalArgumentException.class)
    @ResponseBody
    public Result exceptionHandlerParamsException(IllegalArgumentException e) {
//        log.error("处理参数异常", e);
        return Result.getResult(ResultInfo.PARAME_EMPTY, e.getMessage());
    }


    /**
     * token无效异常
     */
    @ResponseBody
    @ExceptionHandler(IncorrectCredentialsException.class)
    public Result handleTokenException(IncorrectCredentialsException incorrectCredentialsException) {
        log.error("token无效异常", incorrectCredentialsException);
        return Result.getResult(ResultInfo.TOKEN_ERROR);
    }


    /**
     * 参数校验异常处理
     *
     * @return result
     */
    @ResponseBody
    @ExceptionHandler(MissingServletRequestParameterException.class)
    public Result handleMissingParameterException(MissingServletRequestParameterException missingServletRequestParameterException) {
        log.error("参数校验异常", missingServletRequestParameterException);
        return Result.getResult(ResultInfo.MISSING_PARAMS);
    }

    /**
     * 数字格式化异常处理
     *
     * @return result
     */
    @ResponseBody
    @ExceptionHandler(NumberFormatException.class)
    public Result handleNumberFormatException(NumberFormatException numberFormatException) {
        log.error("数字格式化异常", numberFormatException);
        return Result.getResult(ResultInfo.NUMBER_FORMAT_EXCEPTION);
    }


    /**
     * 请求方法错误异常
     *
     * @param e 请求方法错误异常
     * @return
     */
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public Result httpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
        return Result.getResult(ResultInfo.FAILED, "请求方法出错,你请求的方法" + e.getMethod() + ",该接口支持的方法" + e.getSupportedHttpMethods());
    }


    /**
     * 处理其他异常
     *
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Result exceptionHandler(Exception e) {
        log.error("未知异常!原因是:", e);
        return Result.getResult(ResultInfo.SERVER_INTERNAL_EXCEPTION);
    }

}
Spring Boot中,可以通过配置全局异常处理器来捕获和处理应用程序中的异常。以下是配置全局异常处理的步骤: 1. 创建一个全局异常处理器类,例如 `GlobalExceptionHandler`。 ```java @ControllerAdvice public class GlobalExceptionHandler { @ExceptionHandler(Exception.class) public ResponseEntity<ErrorResponse> handleException(Exception ex) { // 处理异常逻辑,可以根据需要进行自定义处理 ErrorResponse errorResponse = new ErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR.value(), ex.getMessage()); return new ResponseEntity<>(errorResponse, HttpStatus.INTERNAL_SERVER_ERROR); } // 可以在此定义其他异常处理方法,处理不同类型的异常 } ``` 2. 在全局异常处理器类上使用 `@ControllerAdvice` 注解,让它成为一个全局异常处理器。 3. 在全局异常处理器类中定义异常处理方法,使用 `@ExceptionHandler` 注解指定需要处理的异常类型。 4. 在异常处理方法中编写具体的异常处理逻辑,例如创建一个自定义的错误响应对象,并返回希望的 HTTP 状态码。 5. 可以在全局异常处理器类中定义多个异常处理方法,用于处理不同类型的异常。 6. 最后,将全局异常处理器类注册到Spring Boot应用程序中。 ```java @SpringBootApplication public class YourApplication { public static void main(String[] args) { SpringApplication.run(YourApplication.class, args); } @Bean public GlobalExceptionHandler globalExceptionHandler() { return new GlobalExceptionHandler(); } } ``` 通过以上步骤,您就可以在应用程序中配置一个全局异常处理器,捕获并处理应用程序中的异常。在处理方法中,您可以根据需要进行自定义异常处理,例如返回自定义的错误响应、记录日志等。
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值