【统一全局异常处理】2. 自定义异常和相关数据结构

故事扮演 提交于 2019-12-26 11:19:35

【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>>

一、该如何设计数据结构

  1. CustomException 自定义异常。核心要素:异常错误编码(200正常,400,500),异常错误信息message。
  2. ExceptionTypeEnum 枚举异常分类,将异常分类固化下来,防止开发人员思维发散。 核心要素 异常分类编码(200正常,400,500),异常分类描述。
  3. AjaxResponse 用于响应Ajax请求。核心要素:是否请求成功 isok;响应code零与非零,零表示成功(200,400,500);响应成功与否信息描述message;响应成功的数据data。
  4. error.html
    另外还需要有一个统一处理CustomException的地方。即@ControllerAdvice和@ExceptionHandler

二、自定义异常及自定义响应数据结构 

异常分类的枚举,把异常分类固化下来

public enum CustomExceptionType {
    /**
     * 定义异常
     */
    USER_INPUT_ERROR(400, "用户输入异常"),
    SYSTEM_ERROR(500, "系统服务异常"),
    OTHER_ERROR(999, "其他未知异常");

    CustomExceptionType(int code, String typeDesc) {
        this.code = code;
        this.typeDesc = typeDesc;
    }

    /**
     * 异常类型中文描述
     */
    private String typeDesc;

    /**
     * code  状态码
     */
    private int code;

    public String getTypeDesc() {
        return typeDesc;
    }

    public int getCode() {
        return code;
    }
}

 

自定义异常

public class CustomException extends RuntimeException {
    /**
     * 异常错误编码
     */
    private int code;

    /**
     * 异常信息
     */
    private String message;

    /**
     * 用户异常
     *
     * @param exceptionTypeEnum
     * @param message
     */
    public CustomException(CustomExceptionType exceptionTypeEnum, String message) {
        this.code = exceptionTypeEnum.getCode();
        this.message = message;
    }

    public int getCode() {
        return code;
    }

    @Override
    public String getMessage() {
        return message;
    }

}

 

统一响应前端的数据结构

@Data
public class AjaxResponse {
    /**
     * ajax请求是否成功
     */
    private boolean isok;
    /**
     * http status code
     */
    private int code;
    /**
     * 请求失败的的提示信息。
     */
    private String message;
    /**
     * 请求成功时,需要响应给前端的数据
     */
    private Object data;

    private AjaxResponse() {

    }

    /**
     * 请求出现异常时的响应数据封装
     * @param e
     * @return
     */
    public static AjaxResponse error(CustomException e) {

        AjaxResponse resultBean = new AjaxResponse();
        resultBean.setIsok(false);
        resultBean.setCode(e.getCode());
        if(e.getCode() == CustomExceptionType.USER_INPUT_ERROR.getCode()){
            resultBean.setMessage(e.getMessage());
        }else if(e.getCode() == CustomExceptionType.SYSTEM_ERROR.getCode()){
            resultBean.setMessage(e.getMessage() + ",系统出现异常,请联系管理员进行处理!");
        }else{
            resultBean.setMessage("系统出现未知异常,请联系管理员进行处理!");
        }
        return resultBean;
    }

    /**
     * 请求成功时的响应数据封装,没有响应数据(比如删除修改成功)
     * @return
     */
    public static AjaxResponse success() {
        AjaxResponse resultBean = new AjaxResponse();
        resultBean.setIsok(true);
        resultBean.setCode(200);
        resultBean.setMessage("success");
        return resultBean;
    }

    /**
     * 请求成功时的响应数据封装,有响应数据(比如查询成功)
     * @param data
     * @return
     */
    public static AjaxResponse success(Object data) {
        AjaxResponse resultBean = new AjaxResponse();
        resultBean.setIsok(true);
        resultBean.setCode(200);
        resultBean.setMessage("success");
        resultBean.setData(data);
        return resultBean;
    }
}

 

三、无异常情况下,如何使用AjaxResponse

例如:更新操作,Controller无需返回额外的数据

    @PutMapping("/api/custom/{id}")
    public AjaxResponse putCustom(@PathVariable("id") Long id) {
        return AjaxResponse.success();
    }

例如:查询接口,Controller需返回结果数据(data可以是任何类型数据)

    @GetMapping("/api/custom/{id}")
    public AjaxResponse getCustom(@PathVariable("id") Long id) {
        Map<String, Object> m = new HashMap<String, Object>();
        m.put("customId", id);
        m.put("customName", "李四");
        m.put("customAge", "女");

        return AjaxResponse.success(m);
    }

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!