Spring Boot全局统一异常处理器

网友投稿 224 2023-01-15

Spring Boot全局统一异常处理器

一、封装统一返回结果类

import com.jiusen.exercise.enums.ErrorEnum;

import com.jiusen.exercise.exception.BusinessException;

import lombok.Getter;

import lombok.Setter;

/**

* @author: Lawson

* @date: 2021/5/11

* @description: TODO 统一的返回结果

*/

@Getter

@Setter

public class AjaxResult {

//是否成功

private Boolean success;

//状态码

private Integer code;

//提示信息

private String msg;

//数据

private Object data;

public AjaxResult() {

}

//自定义返回结果的构造方法

public AjaxResult(Boolean success, Integer code, String msg, Object data) {

this.success = success;

this.code = code;

this.msg = msg;

this.data = data;

}

//自定义异常返回的结果

public static AjaxResult defineError(BusinessException de) {

AjaxResult result = new AjaxResult();

result.setSuccess(false);

result.setCode(de.getErrorCode());

result.setMsg(de.getErrorMsg());

result.setData(null);

return result;

}

//其他异常处理方法返回的结果

public static AjaxResult otherError(ErrorEnum errorEnum){

AjaxResult result = new AjaxResult();

result.setMsg(errorEnum.getErrorMsg());

result.setCode(errorEnum.getErrorCode());

result.setSuccess(false);

result.setData(null);

return result;

NWQAYl }

}

二、自定义异常封装类

import lombok.Getter;

import lombok.Setter;

/**

* @author: Lawson

* @date: 2021/5/11

* @description: TODO 类描述

*/

@Getter

@Setter

public class BusinessException extends RuntimeException{

private static final long serialVersionUID = 1L;

/**

* 错误状态码

*/

protected Integer errorCode;

/**

* 错误提示

*/

protected String errorMsg;

public BusinessException() {

}

public BusinessException(String message, Integer errorCode, String errorMsg) {

super(message);

this.errorCode = errorCode;

this.errorMsg = errorMsg;

}

}

三、错误枚举

**

* @author: Lawson

* @date: 2021/5/11

* @description: TODO 类描述

*/

public enum ErrorEnum {

//数据操作错误定义

SUCCESS(200, "成功"),

NO_PERMISSION(403, "你无权访问"),

NO_Auth(401, "未授权,请登录验证"),

NO_FOUND(404, "未找到资源"),

INTERNAL_SERVER_ERROR(500, "服务器异常, 请联系管理员!");

http:///**

* 错误码

*/

prhttp://ivate Integer errorCode;

/**

* 错误信息

*/

private String errorMsg;

ErrorEnum(Integer errorCode, String errorMsg) {

this.errorCode = errorCode;

this.errorMsg = errorMsg;

}

public Integer getErrorCode() {

return errorCode;

}

public String getErrorMsg() {

return errorMsg;

}

}

四、全局异常处理类

import com.jiusen.exercise.enums.ErrorEnum;

import com.jiusen.exercise.exception.BusinessException;

import com.jiusen.exercise.rest.AjaxResult;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.web.bind.annotation.ExceptionHandler;

import org.springframework.web.bind.annotation.RestControllerAdvice;

/**

* @author: Lawson

* @date: 2021/5/11

* @description: TODO 类描述

*/

@RestControllerAdvice

public class GlobalExceptionHandler {

private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);

/**

* 处理自定义异常

* @param e

* @return

*/

@ExceptionHandler(value = BusinessException.class)

public AjaxResult bizExceptionHandler(BusinessException e) {

log.error(e.getErrorMsg(), e);

return AjaxResult.defineError(e);

}

/**

* 处理其它异常

*/

@ExceptionHandler(value = Exception.class)

public AjaxResult exceptionHandler(Exception e) {

log.error(e.getMessage(), e);

return AjaxResult.otherError(ErrorEnum.INTERNAL_SERVER_ERROR);

}

}

五、测试

import com.jiusen.exercise.enums.ErrorEnum;

import com.jiusen.exercise.exception.BusinessException;

import com.jiusen.exercise.rest.AjaxResult;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.web.bind.annotation.ExceptionHandler;

import org.springframework.web.bind.annotation.RestControllerAdvice;

/**

* @author: Lawson

* @date: 2021/5/11

* @description: TODO 类描述

*/

@RestControllerAdvice

public class GlobalExceptionHandler {

private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);

/**

* 处理自定义异常

* @param e

* @return

*/

@ExceptionHandler(value = BusinessException.class)

public AjaxResult bizExceptionHandler(BusinessException e) {

log.error(e.getErrorMsg(), e);

return AjaxResult.defineError(e);

}

/**

* 处理其它异常

*/

@ExceptionHandler(value = Exception.class)

public AjaxResult exceptionHandler(Exception e) {

log.error(e.getMessage(), e);

return AjaxResult.otherError(ErrorEnum.INTERNAL_SERVER_ERROR);

}

}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Java基础之SpringBoot整合knife4j
下一篇:Java数据结构之哈夫曼树概述及实现
相关文章

 发表评论

暂时没有评论,来抢沙发吧~