码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2020-03-26 01:35:31      阅读:84      评论:0      收藏:0      [点我收藏+]

标签:异常   tcl   value   统一   erro   mapping   boolean   其他   ==   

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开始)

标签:异常   tcl   value   统一   erro   mapping   boolean   其他   ==   

原文地址:https://www.cnblogs.com/dreamShines/p/12571669.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!