-
自定义异常类继承RuntimeException
package com.gen.genonlineclassroom.exception; import lombok.Data; /** * 自定义异常类 */ @Data public class CustomException extends RuntimeException { private Integer code; private String msg; }
-
开发异常处理器ExceptionHandler
package com.gen.genonlineclassroom.exception; import com.gen.genonlineclassroom.enums.ResponseCodeEnum; import com.gen.genonlineclassroom.utils.JsonData; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestControllerAdvice; /** * 异常处理器 * * @author Gen */ @RestControllerAdvice public class CustomExceptionHandler { @ExceptionHandler(value = Exception.class) public JsonData handlerException(Exception e) { if (e instanceof CustomException) { CustomException customException = (CustomException) e; return JsonData.buildError(customException.getCode(), customException.getMsg(), null); } return JsonData.build(ResponseCodeEnum.SYSTEM_EXCEPTION, null); } }
springboot自定义全局异常
于 2023-05-31 11:30:54 首次发布