• 统一异常处理


    1 统一请求返回的最外层对象

    日常处理请求时,异常返回结果与正常返回结果格式不一样,不利于前后端的数据交互,如果不处理也不利于编码。封装一个统一请求返回结果最外层对象是一种比较好的设计思想。

    package com.latiny.pojo;
    
    public class Result<T> {
    
        //提示码
        private Integer code;
        //提示信息
        private String msg;
        //具体内容
        private T data;
    
        public Integer getCode() {
            return code;
        }
    
        public void setCode(Integer code) {
            this.code = code;
        }
    
        public String getMsg() {
            return msg;
        }
    
        public void setMsg(String msg) {
            this.msg = msg;
        }
    
        public T getData() {
            return data;
        }
    
        public void setData(T data) {
            this.data = data;
        }
    }

    2 统一对象类的操作封装到一个工具类,避免重复代码

    package com.latiny.Util;
    import com.latiny.pojo.Result;
    
    public class ResultUtil {
    
        public static Result success(Object object){
            Result result = new Result();
            result.setCode(0);
            result.setMsg("成功");
            result.setData(object);
            return result;
        }
    
        public static Result success(){
            return success(null);
        }
    
        public static Result error(Integer code, String message){
            Result result = new Result();
            result.setCode(code);
            result.setMsg(message);
            result.setData("null");
            return result;
        }
    }

    3 添加异常捕获类

    package com.latiny.handle;
    
    import com.latiny.Util.ResultUtil;
    import com.latiny.exception.GirlException;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.web.bind.annotation.ControllerAdvice;
    import org.springframework.web.bind.annotation.ExceptionHandler;
    import org.springframework.web.bind.annotation.ResponseBody;
    import com.latiny.pojo.Result;
    
    @ControllerAdvice
    public class ExceptionHandle {
    
        private final static Logger logger = LoggerFactory.getLogger(ExceptionHandle.class);
    
        @ExceptionHandler(value = Exception.class)
        @ResponseBody
        public Result handle(Exception e){
            
            if(e instanceof GirlException){
                GirlException girlException = (GirlException)e;
                return ResultUtil.error(girlException.getCode(),girlException.getMessage());
            }else{
                logger.error("系统异常{}", e);
                System.out.println(e.getMessage());
                return ResultUtil.error(-1, "未知错误");
            }
        }
    }

    4 自定义异常类

    针对不同异常,我们有时候不仅需要管理查看异常信息,更希望不同的异常有不同的编号,但是直接抛出的Exception异常是没有编号的,此时我们可以自定义异常。自定义异常可以继承Exception,也可以继承RuntimeException,区别是RuntimeException发生时会对事务进行回滚,Exception不会。

    package com.latiny.exception;
    
    import com.latiny.enums.ResultEnum;
    
    public class GirlException extends RuntimeException {
    
        private Integer code;
    
        public GirlException(ResultEnum resultEnum) {
            super(resultEnum.getMessage());
            this.code = resultEnum.getCode();
        }
    
        public Integer getCode() {
            return code;
        }
    
        public void setCode(Integer code) {
            this.code = code;
        }
    }

     

    5 枚举类统一管理异常编码与提示信息

    package com.latiny.enums;
    
    public enum ResultEnum {
        UNKONW_ERROR(-1, "未知错误"),
        SUCCES(0, "成功"),
        PRIMARY_SCHOOL(100, "年龄异常1: 小学"),
        MIDDLE_SCHOOL(101, "年龄异常2: 初中"),
        GRADUATE(102, "年龄异常3: 高中"),
        ;
        
        private Integer code;
        private String message;
    
        ResultEnum(Integer code, String message) {
            this.code = code;
            this.message = message;
        }
    
        public Integer getCode() {
            return code;
        }
    
        public String getMessage() {
            return message;
        }
    }

    编码时在其他业务逻辑层只需要将异常向调用层抛出即可,即:dao -> service -> controler,最后异常到controller层时,controller将异常交给异常处理器处理。

  • 相关阅读:
    hihocoder 1049 后序遍历
    hihocoder 1310 岛屿
    Leetcode 63. Unique Paths II
    Leetcode 62. Unique Paths
    Leetcode 70. Climbing Stairs
    poj 3544 Journey with Pigs
    Leetcode 338. Counting Bits
    Leetcode 136. Single Number
    Leetcode 342. Power of Four
    Leetcode 299. Bulls and Cows
  • 原文地址:https://www.cnblogs.com/Latiny/p/9003662.html
Copyright © 2020-2023  润新知