SpringBoot全局异常处理

全局异常处理的好处:不用逐个方法手动try-catch异常,提高开发效率,降低代码冗余

思路:@ControllerAdvice + @ExceptionHandler

@ExceptionHandler 注解用于拦截异常,可以通过该注解实现自定义异常处理。其中,@ExceptionHandler 配置的 value 指定需要拦截的异常类型。

@ExceptionHandler(value = SaTokenException.class)

@ControllerAdvice 注解用于定义@ExceptionHandler,并应用到所有@RequestMapping中。

如果全部异常处理返回json,那么可以使用 @RestControllerAdvice 代替 @ControllerAdvice ,这样在方法上就可以不需要添加 @ResponseBody。

下面使用@RestControllerAdvice和@ExceptionHandler展示全局异常处理的demo。

1. 编写全局异常处理类

package com.cjl.mss.common.exception;

@RestControllerAdvice
public class GlobalExceptionResolver{

    @ExceptionHandler(SaTokenException.class)
    public Object handleAuthorizationException(HttpServletRequest request, SaTokenException e)
    {
        // 登录认证异常
        if(e instanceof NotLoginException){
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(e.getMessage());
        }
        // 权限认证异常
        else if (e instanceof NotPermissionException || e instanceof NotRoleException || e instanceof NotSafeException){
            return ResponseEntity.status(HttpStatus.FORBIDDEN).body(e.getMessage());
        }
        // 其它异常
        else {
            return ResponseEntity.status(HttpStatus.FORBIDDEN).body("oops, error!");
        }
    }

    /**
     * 请求方式不支持
     */
    @ExceptionHandler({ HttpRequestMethodNotSupportedException.class })
    public ResponseEntity<String> handleException(HttpRequestMethodNotSupportedException e)
    {
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("不支持' " + e.getMethod() + "'请求");
    }


    /**
     * 系统异常
     */
    @ExceptionHandler(Exception.class)
    public ResponseEntity<String> handle(Exception e){
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("系统异常:" + e.getMessage());
    }


    /**
     * 拦截未知的运行时异常
     */
    @ExceptionHandler(RuntimeException.class)
    public ResponseEntity<String> notFount(RuntimeException e)
    {
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("运行时异常:" + e.getMessage());
    }

}

2. 注入Spring容器

package com.cjl.mss.auth;

@ComponentScan({"com.cjl.mss.auth", "com.cjl.mss.common"})
@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class})
public class MSSAuthApplication {
    public static void main(String[] args) {
        SpringApplication.run(MSSAuthApplication.class,args);
    }
}

3. 补充

  1. 若全局异常处理类和SpringBoot启动类在同一个包下,则无需使用@ComponentScan注解。
  2. ResponseEntity对象是Spring对请求响应的封装。它继承了HttpEntity对象,包含了Http的响应码(httpstatus)、响应头(header)、响应体(body)三个部分。可以根据业务需要自定义返回实体。
  3. 可以自定义异常类,实现更贴合业务的异常拦截。
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值