12)SpringBoot 异常枚举、自定义异常、统一的全局异常处理

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

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

  12)SpringBoot 异常枚举、自定义异常、统一的全局异常处理

 

 

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;
    }
}

 

 

2 编写自定义异常

  技巧01:继承运行异常

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

  12)SpringBoot 异常枚举、自定义异常、统一的全局异常处理

 

 

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();
    }
}

 

 

3 全局统一异常处理

  3.1 springBoot默认的异常处理

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

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

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

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

  3.2 创建统一异常处理类

    12)SpringBoot 异常枚举、自定义异常、统一的全局异常处理

 

 

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();
    }
}

 

    技巧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 {};
}

 

    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 {};
}

原文链接https://www.cnblogs.com/NeverCtrl-C/p/8178159.html

关注公众号,将会有更多精彩每天送达:

公众号内有精彩内容,可以提现

                                                  12)SpringBoot 异常枚举、自定义异常、统一的全局异常处理