zoukankan      html  css  js  c++  java
  • 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);
    }
    }
  • 相关阅读:
    Chrome滚动条透明的问题
    JavaScript版的简单动画
    关于AS3的事件移除释疑
    JavaScript的Hook
    JavaScript正则表达式的零宽断言
    webgame开发中的文件解密
    GMIC2011:熊俊谈从数据看iOS移动应用开发
    世上最伟大的十个公式
    OS X Lion 正式版制作安装盘全新安装方法说明
    市面上VB.NET的书比较少,推荐一本,下载地址在下面!
  • 原文地址:https://www.cnblogs.com/god-monk/p/11461462.html
Copyright © 2011-2022 走看看