自定义公共异常处理(spring3.2开始)

Posted lost_s

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了自定义公共异常处理(spring3.2开始)相关的知识,希望对你有一定的参考价值。

idea已安装lombok插件

/**
 * 数据响应对象
 *    {
 *      success :是否成功
 *      code    :返回码
 *      message :返回信息
 *      //返回数据
 *      data:  :{
 *
 *      }
 *    }
 *    lombok插件注解:@Data ; @NoArgsConstructor
 */
@Data
@NoArgsConstructor
public class Result {

    private boolean success;//是否成功
    private Integer code;// 返回码
    private String message;//返回信息
    private Object data;// 返回数据

    public Result(ResultCode code) {
        this.success = code.success;
        this.code = code.code;
        this.message = code.message;
    }

    public Result(ResultCode code,Object data) {
        this.success = code.success;
        this.code = code.code;
        this.message = code.message;
        this.data = data;
    }

    public Result(Integer code,String message,boolean success) {
        this.code = code;
        this.message = message;
        this.success = success;
    }

    public static Result SUCCESS(){
        return new Result(ResultCode.SUCCESS);
    }

    public static Result ERROR(){
        return new Result(ResultCode.CUSTOM_ERROR);
    }

    public static Result FAIL(){
        return new Result(ResultCode.FAIL);
    }
}

 

/**
 *    枚举类:公共的返回码
 *    返回码code:
 *    成功:10010
 *    失败:10086
 *    错误:10000
 */
public enum ResultCode {

    SUCCESS(true,10010,"成功!"),
    FAIL(false,10086,"失败"),
    CUSTOM_ERROR(false,10000,"自定义错误!");

    //操作是否成功
    boolean success;
    //操作代码
    int code;
    //提示信息
    String message;

    ResultCode(boolean success,int code, String message){
        this.success = success;
        this.code = code;
        this.message = message;
    }

    public boolean success() {
        return success;
    }

    public int code() {
        return code;
    }

    public String message() {
        return message;
    }

}

 

/**
 * 自定义异常类
 * lombok注解:@Getter
 */
@Getter
public class CustomException extends Exception  {

    private ResultCode resultCode;

    public CustomException(ResultCode resultCode) {
        this.resultCode = resultCode;
    }
}

 

/**
 * 自定义的公共异常处理器:对异常统一处理
 *  @ControllerAdvice,Spring3.2提供的新注解
 */
@ControllerAdvice
public class BaseExceptionHandler {

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Result error(HttpServletRequest request, HttpServletResponse response,Exception e) {
        if(e.getClass() == CustomException.class) {//抛出自定义异常
            CustomException ce = (CustomException) e;
            Result result = new Result(ce.getResultCode());
            return result;
        }else{//其他异常统一处理为FAIL(false,10086,"失败")
            Result result = new Result(ResultCode.FAIL);
            return result;
        }
    }
}

 

    //在Controller中测试
    @RequestMapping(value="",method = RequestMethod.GET)
    public Result test() throws CustomException {
        //BaseExceptionHandler类中将抛出自定义异常CUSTOM_ERROR(false,10000,"自定义错误!");
        throw new CustomException(ResultCode.CUSTOM_ERROR);
    }
    @RequestMapping(value="",method = RequestMethod.GET)
    public Result test(){
        //构造异常:BaseExceptionHandler类中将统一处理为FAIL(false,10086,"失败")
        int i = 1/0;
        //业务操作TODO
        Result result = new Result(ResultCode.SUCCESS);//上面没有异常则返回成功SUCCESS(true,10010,"成功!")
        return result;
    }

 

以上是关于自定义公共异常处理(spring3.2开始)的主要内容,如果未能解决你的问题,请参考以下文章

PHP 异常处理

php异常处理

springboot 异常注解

从零开始的Java开发1-5-1 异常:分类异常处理try-catch-finally多重catchfinally与returnthrowsthrow关键字自定义异常类异常链

从 MVC 到前后端分离(下)

JAVA之旅——异常的概述,Try-Catch,异常声明Throws,多异常处理,自定义异常,Throw和Throws的区别