码迷,mamicode.com
首页 > 其他好文 > 详细

全局捕获异常

时间:2020-09-18 02:03:36      阅读:32      评论:0      收藏:0      [点我收藏+]

标签:static   ram   rac   code   错误码   返回   处理   set   oba   

首先需要一个公共的返回响应类

package com.wd.common.response;

import com.wd.common.exception.CodeMsg;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;

@ApiModel(value = "返回响应类")
public class ResponseModel<T extends Object> implements Serializable {

    @ApiModelProperty(value = "响应码")
    private Integer code;
    @ApiModelProperty(value = "响应消息")
    private String msg;
    @ApiModelProperty(value = "响应数据")
    private T data;


    public static <T> ResponseModel<T> success(T data){
        return new ResponseModel<T>(data);
    }

    public static <T> ResponseModel<T> error(CodeMsg cm){
        return new ResponseModel<T>(cm);
    }


    private ResponseModel(T data) {
        this.code=0;
        this.msg="success";
        this.data=data;
    }

    private ResponseModel(CodeMsg cm) {
        if(cm==null){
            return;
        }
        this.code=cm.getCode();
        this.msg=cm.getMsg();
    }

    public ResponseModel(String msg, T data) {
        this.code = 0;
        this.msg = msg;
        this.data = data;
    }

    public ResponseModel(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = 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;
    }
}
package com.wd.common.exception;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

@ApiModel(value = "异常错误码", description = "")
public class CodeMsg {
    @ApiModelProperty(value = "响应码")
    private Integer code;
    @ApiModelProperty(value = "响应消息")
    private String msg;

    //通用的错误码
    public static CodeMsg SERVER_ERROR = new CodeMsg(100001, "服务端异常,程序员小哥正在维护中!");
    public static CodeMsg BIND_ERROR = new CodeMsg(100002, "参数校验异常!");
    public static CodeMsg NOT_PARAMETER = new CodeMsg(100002, "缺少关键参数字段!");
    public static CodeMsg NOT_NULL = new CodeMsg(100002, "未查询到数据!");





    public CodeMsg() {
    }

    public CodeMsg(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public CodeMsg fillArgs(Object... args) {
        int code = this.code;
        String message = String.format(this.msg, args);
        return new CodeMsg(code, message);
    }

    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;
    }
}
package com.wd.common.exception;

public class GlobalException extends RuntimeException{

    private static final long serialVersionUID = 1L;

    private CodeMsg codeMsg;

    public GlobalException(CodeMsg codeMsg) {
        super(codeMsg.toString());
        this.codeMsg = codeMsg;
    }

    public CodeMsg getCm() {
        return codeMsg;
    }
}
package com.wd.common.exception;

import com.wd.common.response.ResponseModel;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.net.BindException;
import java.util.List;

@ControllerAdvice //该注解定义全局异常处理类
//@ControllerAdvice(basePackages ="com.wd.business.service") // 指定包
@ResponseBody
public class GlobalExceptionHandler {
    @ExceptionHandler(value=Exception.class) //该注解声明异常处理方法
    public ResponseModel<String> exceptionHandler(HttpServletRequest request, Exception e){
        e.printStackTrace();
        if (e instanceof GlobalException){
            GlobalException ex = (GlobalException) e;
            return ResponseModel.error(ex.getCm());
        }else if (e instanceof BindException){
            //对于绑定异常的处理,使用jsr303中的自定义注解抛出的异常属于绑定异常
            BindException ex = (BindException) e;
            String message = ex.getMessage();
            return ResponseModel.error(CodeMsg.BIND_ERROR.fillArgs(message));
        }else {
            return ResponseModel.error(CodeMsg.SERVER_ERROR);
        }

    }
}

 

全局捕获异常

标签:static   ram   rac   code   错误码   返回   处理   set   oba   

原文地址:https://www.cnblogs.com/yiMro/p/13672652.html

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