SpringBoot中如何有效处理业务逻辑异常?

2026-05-24 02:341阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计813个文字,预计阅读时间需要4分钟。

SpringBoot中如何有效处理业务逻辑异常?

目录 + 部分异常 + 全局异常 + 在Spring Boot项目中,除了设置错误页面,还可以通过注解实现错误处理。+ 部分异常:在控制器类中,添加方法处理特定异常。+ 全局异常:在全局异常处理器中,通过注解@ExceptionHandler处理。+ 局部异常处理器:在控制器类中,添加方法并使用@ExceptionHandler注解。

目录
  • 局部异常
  • 全局异常

在Spring Boot项目中除了设置错误页面,还可以通过注解实现错误处理。

局部异常

局部异常:

在控制器类中添加一个方法,结合@ExceptionHandler。但是只能对当前控制器中方法出现异常进行解决。

引入lombok依赖

<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency>

1.创建异常信息类

package com.yyl.firstdemo.exception; import lombok.Data; @Data public class ExceptionMessage { private String code; private String message; }

2.在controller中设置异常处理

package com.yyl.firstdemo.controller; import com.yyl.firstdemo.exception.ExceptionMessage; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class ErrorController { @RequestMapping("/test") public String testError(){ System.out.println(5/0); return "500"; } @ExceptionHandler(ArithmeticException.class) @ResponseBody public ExceptionMessage arithmeticException(Exception e){ ExceptionMessage exceptionMessage = new ExceptionMessage(); exceptionMessage.setCode("500"); exceptionMessage.setMessage(e.getMessage()); return exceptionMessage; } }

@ExceptionHandler的参数为发生异常的类型。如果controller的方法中捕获到了这种异常,就会走@ExceptionHandler表示的方法arithmeticException(),在方法参数中,可以获取异常对象。

最终执行结果:

当访问test的controller方法时,会出现除0异常,就会走异常处理方法,封装异常信息,返回,错误类封装的状态信息。

全局异常

新建全局异常类,通过@ControllerAdvice结合@ExceptionHandler。当全局异常处理和局部处理同时存在时,局部生效(就近原则)

编写全局异常处理器:

package com.yyl.firstdemo.exception; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseBody; @ControllerAdvice public class GlobalExceptionHandleController { //发生除0异常时,会执行该方法 @ExceptionHandler(ArithmeticException.class) @ResponseBody public ExceptionMessage arithmeticException(Exception e){ ExceptionMessage exceptionMessage = new ExceptionMessage(); exceptionMessage.setCode("500"); exceptionMessage.setMessage(e.getMessage()); return exceptionMessage; } //发生空指针异常时会执行该方法 @ExceptionHandler(NullPointerException.class) @ResponseBody public ExceptionMessage nullPointerException(NullPointerException e){ ExceptionMessage exceptionMessage = new ExceptionMessage(); exceptionMessage.setCode("500"); exceptionMessage.setMessage(e.getMessage()); return exceptionMessage; } //发生其他未知异常时,会执行该方法 @ExceptionHandler(Exception.class) @ResponseBody public ExceptionMessage otherException(){ ExceptionMessage exceptionMessage = new ExceptionMessage(); exceptionMessage.setCode("500"); exceptionMessage.setMessage("发生了其他未知异常!"); return exceptionMessage; } }

编写controller,增加两个方法:

package com.yyl.firstdemo.controller; import com.yyl.firstdemo.exception.ExceptionMessage; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class ErrorController { @RequestMapping("/test") public String testError(){ System.out.println(5/0); return "500"; } @RequestMapping("/testNull") public String testNull(){ String s = null; System.out.println(s.length()); return null; } @RequestMapping("testOther") public String testOther(){ int[] arr = new int[3]; System.out.println(arr[5]); return null; } }

测试结果如下:

我们再加上局部异常,再进行测试:

我们发现就近原则生效,异常被局部异常处理器捕获处理

SpringBoot中如何有效处理业务逻辑异常?

到此这篇关于SpringBoot业务逻辑异常的处理方法介绍的文章就介绍到这了,更多相关SpringBoot业务逻辑异常内容请搜索自由互联以前的文章或继续浏览下面的相关文章希望大家以后多多支持自由互联!

本文共计813个文字,预计阅读时间需要4分钟。

SpringBoot中如何有效处理业务逻辑异常?

目录 + 部分异常 + 全局异常 + 在Spring Boot项目中,除了设置错误页面,还可以通过注解实现错误处理。+ 部分异常:在控制器类中,添加方法处理特定异常。+ 全局异常:在全局异常处理器中,通过注解@ExceptionHandler处理。+ 局部异常处理器:在控制器类中,添加方法并使用@ExceptionHandler注解。

目录
  • 局部异常
  • 全局异常

在Spring Boot项目中除了设置错误页面,还可以通过注解实现错误处理。

局部异常

局部异常:

在控制器类中添加一个方法,结合@ExceptionHandler。但是只能对当前控制器中方法出现异常进行解决。

引入lombok依赖

<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency>

1.创建异常信息类

package com.yyl.firstdemo.exception; import lombok.Data; @Data public class ExceptionMessage { private String code; private String message; }

2.在controller中设置异常处理

package com.yyl.firstdemo.controller; import com.yyl.firstdemo.exception.ExceptionMessage; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class ErrorController { @RequestMapping("/test") public String testError(){ System.out.println(5/0); return "500"; } @ExceptionHandler(ArithmeticException.class) @ResponseBody public ExceptionMessage arithmeticException(Exception e){ ExceptionMessage exceptionMessage = new ExceptionMessage(); exceptionMessage.setCode("500"); exceptionMessage.setMessage(e.getMessage()); return exceptionMessage; } }

@ExceptionHandler的参数为发生异常的类型。如果controller的方法中捕获到了这种异常,就会走@ExceptionHandler表示的方法arithmeticException(),在方法参数中,可以获取异常对象。

最终执行结果:

当访问test的controller方法时,会出现除0异常,就会走异常处理方法,封装异常信息,返回,错误类封装的状态信息。

全局异常

新建全局异常类,通过@ControllerAdvice结合@ExceptionHandler。当全局异常处理和局部处理同时存在时,局部生效(就近原则)

编写全局异常处理器:

package com.yyl.firstdemo.exception; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseBody; @ControllerAdvice public class GlobalExceptionHandleController { //发生除0异常时,会执行该方法 @ExceptionHandler(ArithmeticException.class) @ResponseBody public ExceptionMessage arithmeticException(Exception e){ ExceptionMessage exceptionMessage = new ExceptionMessage(); exceptionMessage.setCode("500"); exceptionMessage.setMessage(e.getMessage()); return exceptionMessage; } //发生空指针异常时会执行该方法 @ExceptionHandler(NullPointerException.class) @ResponseBody public ExceptionMessage nullPointerException(NullPointerException e){ ExceptionMessage exceptionMessage = new ExceptionMessage(); exceptionMessage.setCode("500"); exceptionMessage.setMessage(e.getMessage()); return exceptionMessage; } //发生其他未知异常时,会执行该方法 @ExceptionHandler(Exception.class) @ResponseBody public ExceptionMessage otherException(){ ExceptionMessage exceptionMessage = new ExceptionMessage(); exceptionMessage.setCode("500"); exceptionMessage.setMessage("发生了其他未知异常!"); return exceptionMessage; } }

编写controller,增加两个方法:

package com.yyl.firstdemo.controller; import com.yyl.firstdemo.exception.ExceptionMessage; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class ErrorController { @RequestMapping("/test") public String testError(){ System.out.println(5/0); return "500"; } @RequestMapping("/testNull") public String testNull(){ String s = null; System.out.println(s.length()); return null; } @RequestMapping("testOther") public String testOther(){ int[] arr = new int[3]; System.out.println(arr[5]); return null; } }

测试结果如下:

我们再加上局部异常,再进行测试:

我们发现就近原则生效,异常被局部异常处理器捕获处理

SpringBoot中如何有效处理业务逻辑异常?

到此这篇关于SpringBoot业务逻辑异常的处理方法介绍的文章就介绍到这了,更多相关SpringBoot业务逻辑异常内容请搜索自由互联以前的文章或继续浏览下面的相关文章希望大家以后多多支持自由互联!