• Springboot+异常处理


    1.定义异常接口

    public interface ResultCode {
    boolean success();
    int code();
    String msg();
    }

    2.定义异常枚举

    @ToString
    public enum CommonCode implements ResultCode {

    SUCCESS(true,200,"请求成功!"),
    FAILED(false,9999,"请求失败!"),
    INVALID_PARAM(false,9001,"非法入参!");
    boolean success;
    int code;
    String msg;

    CommonCode(boolean success, int code, String msg) {
    this.success = success;
    this.code = code;
    this.msg = msg;
    }
    @Override
    public boolean success() {
    return success;
    }
    @Override
    public int code() {
    return code;
    }
    @Override
    public String msg() {
    return msg;
    }
    }

    3.返回基本数据类型

    public class ResponseResult{

    private boolean success;
    private int code;
    private String message;
    private Object data;

    public ResponseResult(){}

    public ResponseResult(boolean success, int code, String message, Object data) {
    this.success = success;
    this.code = code;
    this.message = message;
    this.data = data;
    }

    public ResponseResult(ResultCode resultCode) {
    this.success = resultCode.success();
    this.code = resultCode.code();
    this.message = resultCode.msg();
    }
    //getter setter
    }

    4.自定义异常

    public class CustomException extends RuntimeException {
    private ResultCode resultCode;
    public ResultCode getResultCode() {
    return resultCode;
    }
    public CustomException(ResultCode resultCode) {
    //异常信息为错误代码+异常信息(自定义我们的异常信息格式)
    this.resultCode = resultCode;
    }
    }

    5.异常转化

    public class ExceptionCast {
    public static void cast(ResultCode resultCode){
    throw new CustomException(resultCode);
    }
    }

    6.异常统一处理

    @ControllerAdvice
    public class ExceptionCatch {
    //捕获 CustomException异常
    @ExceptionHandler(CustomException.class)
    @ResponseBody
    public ResponseResult customException(CustomException e) {
    e.printStackTrace();
    ResultCode resultCode = e.getResultCode();
    ResponseResult rr = new ResponseResult(resultCode);
    return rr;
    }

    //使用EXCEPTIONS存放异常类型和错误代码映射 ImmutableMap的特点是一旦创建不可改变 并且线程安全
    // 定义map 配置异常类型所对应的错误代码
    private static ImmutableMap<Class<? extends Throwable>, ResultCode> EXCEPTIONS;
    //定义map的builder对象 去构建ImmutableMap对象
    protected static ImmutableMap.Builder<Class<? extends Throwable>, ResultCode> builder = ImmutableMap.builder();

    //使用build来构建一个y异常类型和错误代码异常
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public ResponseResult exception(Exception e) {
    e.printStackTrace();
    if (EXCEPTIONS == null) {
    EXCEPTIONS = builder.build(); //EXCEPTIONS构建成功
    }
    //从EXCEPTIONS中找异常类型所对因的错误代码 如果找到了将错误代码相应给用户 如果找不到响应9999异常
    ResultCode resultCode = EXCEPTIONS.get(e.getClass());
    if (resultCode != null) {
    return new ResponseResult(resultCode);
    } else {
    return new ResponseResult(CommonCode.FAILED);
    }
    }

    static {
    builder.put(HttpMessageNotReadableException.class, CommonCode.INVALID_PARAM);
    }
    }

    7.SpringBoot起动扫面自定义切面类

    @SpringBootApplication
    @ComponentScan(basePackages = {"exception","demo.controller"})
    public class ActivitiApplication {
    public static void main(String[] args) {
    SpringApplication.run(ActivitiApplication.class, args);
    }
    }
  • 相关阅读:
    寒假记录六
    寒假记录5
    寒假记录4
    寒假记录3
    寒假记录2
    寒假记录1
    hive数据库课堂测试
    第一周
    个人总结
    课程总结
  • 原文地址:https://www.cnblogs.com/god-monk/p/11461462.html
Copyright © 2020-2023  润新知