统一异常处理

2017-09-02  本文已影响0人  我弟是个程序员
  1. 首先,利用枚举,来定义异常类型。定义枚举ResultEnum:
package org.sqlb.enums;

/**
 * 利用枚举来 指定错误码
 */
public enum ResultEnum {
    SUCCESS_RESULT(1,"成功"),
    ERROR_RESULT(2,"参数错误"),
    ;

    private Integer status;
    private String message;

    ResultEnum(Integer status, String message) {
        this.status = status;
        this.message = message;
    }

    public Integer getStatus() {
        return status;
    }

    public String getMessage() {
        return message;
    }
}

  1. 自定义异常,新建CustomException类:
package org.sqlb.exception;

import org.sqlb.enums.ResultEnum;

/**
 * 自定义异常类,添加一个status字段
 * 这里只能继承 RuntimeException 类,因为,spring只对该异常做了事物回滚
 */
public class CustomException extends RuntimeException {

    private Integer status;

    public CustomException(ResultEnum resultEnum) {
        super(resultEnum.getMessage());
        this.status = resultEnum.getStatus();
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }
}
  1. 统一处理异常,定义ExceptionHandle类。之所以统一定义异常处理类,是因为,我们可以通过catch异常,来给前端提供一个规范的数据返回引导,便于前端对异常进行处理,从而给用户一个较好的体验。
package org.sqlb.handle;

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 org.sqlb.bean.Result;
import org.sqlb.exception.CustomException;
import org.sqlb.utils.ResultUtil;

/**
 * 统一异常处理
 */

@ControllerAdvice
public class ExceptionHandle {

    private static final Logger logger = LoggerFactory.getLogger(ExceptionHandle.class);

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Result handle(Exception e) {
        //在异常里面进行返回数据的处理
        if (e instanceof CustomException) {
            CustomException ex = (CustomException) e;
            return ResultUtil.error(ex.getStatus(), e.getMessage());
        }
        logger.info("[系统异常]" ,e);
        return ResultUtil.error(-1, "位置错误");
    }
}
附录:

Result类:

package org.sqlb.bean;

/**
 *  请求返回结果 统一结构 类
 */
public class Result<T> {
    private Integer status;
    private String message;
    private T result;

    public Result() {
    }

    public Result(Integer status, String message, T result) {
        this.status = status;
        this.message = message;
        this.result = result;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getResult() {
        return result;
    }

    public void setResult(T result) {
        this.result = result;
    }
}

ResultUtil类:

package org.sqlb.utils;

import org.sqlb.bean.Result;

/**
 *统一处理response结果
 */
public class ResultUtil {


    public static Result success(Object obj) {
        Result result = new Result();
        result.setResult(obj);
        result.setStatus(1);
        result.setMessage("成功");
        return result;
    }

    public static Result success() {
        return success(null);
    }

    public static Result error(Integer status,String message) {
        Result result = new Result();
        result.setStatus(status);
        result.setMessage(message);
        return result;
    }
}

上一篇下一篇

猜你喜欢

热点阅读