spring全局异常处理 自定义返回数据结构

在写api接口中,正常返回和异常错误返回我们都希望很清楚的将这些信息清楚的返回给用户,出现异常情况下需要清楚的知道是参数异常还是未知异常,而不是返回一个不正确的数据结构。

所以此处只针对写api接口时异常处理:

1、先自定义一个我们需要的异常类,注意要继承RuntimeException类。

public class ApplicationException extends RuntimeException {
    private Integer errorId;

    public ApplicationException(String message) {
        super(message);
        this.errorId = BasicErrorCodeEnum.INTERNAL_SERVER_ERROR.getCode();
    }

    public ApplicationException(String message, Throwable cause) {
        super(message, cause);
        this.errorId = BasicErrorCodeEnum.INTERNAL_SERVER_ERROR.getCode();
        LogFactory.getLog().error("### error ###:: " + message, cause);
    }

    public ApplicationException(ErrorCodeEnum errorCodeEnum) {
        super(errorCodeEnum.getMsg());
        this.errorId = errorCodeEnum.getCode();
    }

    public ApplicationException(ErrorCodeEnum enums, String message) {
        super(message);
        this.errorId = enums.getCode();
    }

    public ApplicationException(Integer errorId, String message, Throwable cause) {
        super(message, cause);
        this.errorId = errorId;
    }

    public ApplicationException(Integer errorId) {
        this.errorId = errorId;
    }

    public Integer getErrorId() {
        return this.errorId;
    }
}

2、异常返回数据结构

public class ExceptionResponse {

    private Integer code ;
    private String msg = "";

    public ExceptionResponse() {
    }

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

    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;
    }
}

3、异常拦截拦截类即controller增强类,这里指定拦截异常,未知默认的异常等等。

@ControllerAdvice(annotations = RestController.class)
@ResponseBody
public class GlobalExceptionHandler {
    private static final Log LOG = LogFactory.getLog(GlobalExceptionHandler.class);

    @ExceptionHandler(value = UserException.class)
    public ExceptionResponse defaultErrorHandler(HttpServletRequest req, UserException e) {
        return new ExceptionResponse(e.getErrorEnum().getCode(), e.getErrorEnum().getMsg());
    }


    @ExceptionHandler(value = HttpMessageNotReadableException.class)
    public ExceptionResponse defaultErrorHandler(HttpServletRequest req, HttpMessageNotReadableException e) {
        return new ExceptionResponse(CommonCodeEnum.BAD_REQUES.getCode(), CommonCodeEnum.BAD_REQUES.getMsg());
    }

    @ExceptionHandler(value = Exception.class)
    public ExceptionResponse defaultErrorHandler(HttpServletRequest req, Exception e) {
        return new ExceptionResponse(CommonCodeEnum.INTERNAL_SERVER_ERROR.getCode(), CommonCodeEnum.INTERNAL_SERVER_ERROR.getMsg());
    }

    @ExceptionHandler(value = ApplicationException.class)
    public ExceptionResponse defaultErrorHandler(HttpServletRequest req, ApplicationException e) {
        return new ExceptionResponse(e.getErrorId(), e.getMessage());
    }


}

  

原文地址:https://www.cnblogs.com/likun10579/p/9359855.html