zoukankan      html  css  js  c++  java
  • SpringBoot07 异常枚举、自定义异常、统一的全局异常处理

    1 异常编号和提示信息统一管理

      利用枚举来实现异常的统一管理

      

    package cn.xiangxu.springboottest.enums;
    
    import lombok.Getter;
    
    /**
     * Girl相关异常
     */
    @Getter
    public enum GirlEnum {
        CONTENT_EMPTY(11, "无任何girl数据")
        ;
        private Integer code;
        private String message;
    
        GirlEnum(Integer code, String message) {
            this.code = code;
            this.message = message;
        }
    }
    View Code

    2 编写自定义异常

      技巧01:继承运行异常

      技巧02:利用枚举来传递异常编号和异常信息,做到异常编号和信息的统一管理

      

    package cn.xiangxu.springboottest.exceptions;
    
    import cn.xiangxu.springboottest.enums.GirlEnum;
    import lombok.Data;
    
    /**
     * Girl相关异常类
     */
    @Data
    public class GirlException extends RuntimeException {
        private Integer code;
    
        public GirlException(GirlEnum girlEnum) {
            super(girlEnum.getMessage());
            this.code = girlEnum.getCode();
        }
    }
    View Code

    3 全局统一异常处理

      3.1 springBoot默认的异常处理

        springboot项目在出现异常后,会自动映射到 /error ;当有异常抛出后就会调转到 /error 对应的地方进行处理,并且 /error 请求有一个全局错误页面来展示异常信息

        坑01:对于前后端分离的项目来说,如果跳转到 /error 时不会返回错误页面的

        坑02:错误页面对于用户来说并不友好,应该返回json数据,前端通过json数据去展现响应的错误页面

        技巧01:创建一个统一异常处理类,该类可以拦截异常并向前台返回json格式的异常信息

      3.2 创建统一异常处理类

        

    package cn.xiangxu.springboottest.exceptions.handler;
    
    import cn.xiangxu.springboottest.exceptions.GirlException;
    import cn.xiangxu.springboottest.model.dataViewModel.ResultViewModel;
    import cn.xiangxu.springboottest.utils.ResultViewModelUtil;
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.web.bind.annotation.ControllerAdvice;
    import org.springframework.web.bind.annotation.ExceptionHandler;
    import org.springframework.web.bind.annotation.ResponseBody;
    import org.springframework.web.bind.annotation.RestController;
    
    @ControllerAdvice(annotations = RestController.class)
    @ResponseBody
    @Slf4j
    public class RestExceptionHandler {
    
        @ExceptionHandler(value = GirlException.class)
        public ResultViewModel girlExceptionHandler(GirlException e) {
            log.info("Girl相关异常 => {}",e.getMessage());
            return ResultViewModelUtil.error(e.getCode(), e.getMessage());
        }
    
        @ExceptionHandler(value = RuntimeException.class)
        public ResultViewModel runtimeExceptionHandler(RuntimeException e) {
            log.info(e.getMessage());
            return ResultViewModelUtil.error();
        }
    }
    View Code

        技巧01:系统异常时返回的异常编号为 -1 ,返回的异常信息为 “系统正在维护”;不能将原始的异常信息返回

        技巧02:自己创建的异常按照自己编写的异常枚举信息返回即可

        3.2.1 @ControllerAdvice

          用来配置需要进行异常处理的包和注解类型

    //
    // Source code recreated from a .class file by IntelliJ IDEA
    // (powered by Fernflower decompiler)
    //
    
    package org.springframework.web.bind.annotation;
    
    import java.lang.annotation.Annotation;
    import java.lang.annotation.Documented;
    import java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    import org.springframework.core.annotation.AliasFor;
    import org.springframework.stereotype.Component;
    
    @Target({ElementType.TYPE})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    @Component
    public @interface ControllerAdvice {
        @AliasFor("basePackages")
        String[] value() default {};
    
        @AliasFor("value")
        String[] basePackages() default {};
    
        Class<?>[] basePackageClasses() default {};
    
        Class<?>[] assignableTypes() default {};
    
        Class<? extends Annotation>[] annotations() default {};
    }
    View Code

        3.2.2 @ResponseBody

          指定该异常处理类返回的是JSON格式数据

        3.2.3 @Slf4j

          日志注解

          技巧01:需要导入lomboc依赖包

        3.2.4 @ExceptionHandler

          用于指定需要处理的异常类型

    //
    // Source code recreated from a .class file by IntelliJ IDEA
    // (powered by Fernflower decompiler)
    //
    
    package org.springframework.web.bind.annotation;
    
    import java.lang.annotation.Documented;
    import java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    
    @Target({ElementType.METHOD})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    public @interface ExceptionHandler {
        Class<? extends Throwable>[] value() default {};
    }
    View Code

    4 参考资料

      参考01     参考02      参考03      参考04      

  • 相关阅读:
    突然又想起了这首诗
    安装使用Androidx86打造快速流畅的Aandroid开发环境!
    解决电脑没插网线虚拟机无法桥接到主机
    使用WordPress更新通知服务,让搜索引擎知道你更新了,加快收录。
    php+apache+mysql环境配置时apache服务不能开启的解决
    WordPress备份的七种办法
    怎么在网站中正确使用JQuery代码
    如何让自己的博客在各搜索引擎中被搜索出来与快速收录
    轻松查看文件被哪个进程使用
    解密QQ非会员漫游聊天记录
  • 原文地址:https://www.cnblogs.com/NeverCtrl-C/p/8178159.html
Copyright © 2011-2022 走看看