统一异常处理

 

1、GrilController

@GetMapping(value = "/grils/{id}")
    public void getAge(@PathVariable("id") Integer id) throws GrilException{
        grilService.getAge(id);
    }

 

 

2、Service:

    public void getAge(Integer id) throws GrilException{
        Gril gril = grilRepository.findOne(id);
        Integer age = gril.getAge();
        if (age < 10) {
            //返回“你还在上小学吧” code=100
            throw new GrilException(ResultEnum.PRIMARY_SCHOOL);
        }else if(age > 10 && age < 16){
            //返回“你可能在上初中” code=101
            throw new GrilException(ResultEnum.MIDDLE_SCHOOL);
        }

        //如果>16岁,加钱
        //...

    }

 

 

3、GrilException

@Data
public class GrilException extends RuntimeException{

    private Integer code;

//    private String emage;

    //获取父类中的错误信息
    public GrilException(ResultEnum resultEnum) {
        super(resultEnum.getMsg());
        this.code = resultEnum.getCode();
    }

    /*public GrilException(ResultEnum resultEnum){
        this.code = resultEnum.getCode();
        this.emage = resultEnum.getMsg();
    }*/

}

 

 

4、ResultEnum

public enum ResultEnum {
PRIMARY_SCHOOL(100,"你可能还在上小学"), MIDDLE_SCHOOL(101,"你可能在上初中"), ; private Integer code; private String msg; private ResultEnum(Integer code, String msg) { this.code = code; this.msg = msg; } public Integer getCode() { return code; } public String getMsg() { return msg; } }

 

5、ExceptionHandle(异常处理类)

@ControllerAdvice
public class ExceptionHandle {

    /**
     * 方法一
     * @param e
     * @return
     */
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Result handle(Exception e){
        //instanceof用来判断内存中实际对象A是不是B类型
        if (e instanceof GrilException) {
            //必须经过这一行代码,grilException对象才能调用GrilException里的方法
            GrilException grilException = (GrilException) e;
            return ResultUtil.error(grilException.getCode(), grilException.getMessage());
        }else{
            return ResultUtil.error(GrilConstant.code,GrilConstant.grilException);
        }
    }


    /**
     * 方法二:定义两个异常,当请求处理出现异常时会根据异常处理器的配置顺序依次尝试异常匹配和处理
     * @param e
     * @return
     */
    /*@ExceptionHandler(value = GrilException.class)
    @ResponseBody
    public Result GrilExceptionHandle(GrilException e){
         return ResultUtil.error(e.getCode(), e.getMessage());
    }


    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Result exceptionHandle(Exception e){
        return ResultUtil.error(GrilConstant.code,GrilConstant.grilException);
    }*/

}

 

 

6、ResultUtil

package com.imooc.utils;

import java.util.List;


import com.imooc.domain.Result;

/**
 * @author by
 *
 */
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 msg){
        Result result = new Result();
        result.setCode(code);
        result.setMsg(msg);
        result.setData(null);
        return result;
    }
}

 

7、Result

package com.imooc.domain;

/**http请求返回的最外层对象
 * @author by
 *
 */
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;
    }
    
    

}

 

8、GrilConstant

package com.imooc.constant;

public class GrilConstant {
    
    public static String grilException = "未知错误";
    
    public static Integer code = -1;

}

github

 

posted @ 2018-07-09 23:48  艾白羊  阅读(236)  评论(0)    收藏  举报