所有的异常信息进行统一处理。
关于异常相关的知识,请移步到Java基础 - 异常
结果展示
加入测试代码:
请求响应信息:
添加统一异常处理类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| import com.panda.base.exception.custom.PandaException; import com.panda.base.result.vo.ApiResult; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestControllerAdvice;
@RestControllerAdvice public class PandaGlobalExceptionHandler {
@ExceptionHandler({PandaException.class}) public ApiResult runtimeException(PandaException e) { return ApiResult.fail("PandaException:" + e); }
@ExceptionHandler({Exception.class}) public ApiResult Exception(Exception e) { return ApiResult.fail("Exception:" + e); }
}
|