个性化的个人网站简易,学院网站建设分工,如何制作网页教程,手机网站开发调用照片介绍异常的处理方式。在项目中#xff0c;都会进行自定义异常#xff0c;并且都是需要配合统一结果返回进行使用。 1.背景引入
#xff08;1#xff09;背景介绍
为什么要处理异常#xff1f;如果不处理项目中的异常信息#xff0c;前端访问我们后端就是显示访问失败的… 介绍异常的处理方式。在项目中都会进行自定义异常并且都是需要配合统一结果返回进行使用。 1.背景引入
1背景介绍
为什么要处理异常如果不处理项目中的异常信息前端访问我们后端就是显示访问失败的所以我们需要处理。但是单单处理还不够还需要将信息返回给前端因为异常是一类问题所以我们可以统一进行处理也就是统一异常处理。
2没有处理异常时
看一段代码
Slf4j
RequestMapping(/test)
RestController
public class TestController {RequestMapping(/hello)public String hello() {log.info(我被前端调用了嘤嘤嘤~);int a 10/0;return hello;}}
通过url进行访问 后端日志 所以这就是没有进行异常处理的后果
3简单处理异常后
下面这段代码就会对异常进行捕获并且返回异常的具体信息
Slf4j
ResponseBody
ControllerAdvice
public class ExceptionAdvice {ExceptionHandler(Exception.class)public String exception(Exception e) {log.warn(e.getMessage());return e.getMessage();}
}
前端 这样前端就知道了明确的异常信息但是实际异常不会单独使用都是作为一个Message封装到统一结果中进行返回。
2.异常使用方法
我们先在代码中定义一个简单的统一结果返回用于介绍和学习异常
import lombok.Data;Data
public class Result {private String message;private int code;public Result(String message,int code) {this.message message;this.code code;}public static Result fail(String message) {return new Result(message,1314);}}
1统一异常模版
controller
import lombok.extern.slf4j.Slf4j;
import org.ljy.testdemo.common.Result;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;Slf4j
RequestMapping(/test)
RestController
public class TestController {RequestMapping(/hello)public String hello() {int a 10/0;return hello;}RequestMapping(/heihei)public Result heihei() {return Result.fail(只因你实在是太美~);}}
异常捕获 import lombok.extern.slf4j.Slf4j;
import org.ljy.testdemo.common.Result;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;Slf4j
ResponseBody
RestControllerAdvice
public class ExceptionAdvice {ExceptionHandlerpublic Result exception(Exception e) {log.error(e.getMessage(), e);return Result.fail(e.getMessage());}
}
我们通过前端就行访问 即时后端发生了异常前端也能收到格式化的数据大大提高了可读性。
2注解解析 写法一类RestControllerAdvice 方法ExceptionHandler
表示该类下的所有方法返回的都是数据
写法二类ControllerAdvice 方法ResponseBody 方法ExceptionHandler
表示该方法返回数据
写法三类ControllerAdvice 类ResponseBody 方法ExceptionHandler
表示该类下的所有方法返回的都是数据
其他写法可以指定捕获异常的类型也就是在ExceptionHandler后面加上括号指定异常的对象
Slf4j
RestControllerAdvice
public class ExceptionAdvice {ExceptionHandler(Exception.class)public Result exception(Exception e) {log.error(e.getMessage(), e);return Result.fail(e.getMessage());}
} 为什么要加上ResponseBody注解不加的时候默认返回视图就会产生异常也就是下面的效果 3.使用自定义异常
这里的自定义异常也就是定义一个异常类然后作为ExceptionHandler捕获的对象
1先自定义一个普通的异常
步骤实现Exception类或者其子类然后写构造方法记得先初始化父类可以看情况重写几个方法
于是我们得到下面的自定义异常类
public class ApplicationException extends RuntimeException{//自定义的错误结果(里面包含错误码和错误信息)protected Result errorResult;//用于throw new ApplicationException(Result.fail(我走的是构造方法))这种情况public Result getErrorResult() {return errorResult;}/*** 构造方法 用于填充信息*/public ApplicationException(Result errorResult) {super(errorResult.getMessage());this.errorResult errorResult;}public ApplicationException(String message) {super(message);}public ApplicationException(Throwable cause) {super(cause);}public ApplicationException(String message, Throwable cause) {super(message, cause);}
}
2再定义全局异常处理器
这里的全局异常处理器就是上面的统一异常处理只需要多捕获一个自定义异常就可以了。
import lombok.extern.slf4j.Slf4j;
import org.ljy.testdemo.common.Result;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;Slf4j
ResponseBody
ControllerAdvice
public class ExceptionAdvice {ExceptionHandler(ApplicationException.class)public Result applicationExceptionHandler (ApplicationException e) {e.printStackTrace();// 打印日志if (e.getErrorResult() ! null) {return e.getErrorResult();}// 一般不会为null构造方法已经限制住了但是保险一些if (e.getMessage() null || e.getMessage().equals()) {return Result.fail(e.getMessage());}// 返回具体的异常信息return Result.fail(e.getMessage());}//兜底捕获异常ExceptionHandler(Exception.class)public Result exception(Exception e) {log.error(e.getMessage(), e);return Result.fail(e.getMessage());}
}大致情况就这么写接下来解析一下统一异常中的几种校验情况