@ControllerAdvice + @ExceptionHandler 定义全局异常

编程入门 行业动态 更新时间:2024-10-24 07:24:00

@ControllerAdvice + @ExceptionHandler 定义<a href=https://www.elefans.com/category/jswz/34/1765343.html style=全局异常"/>

@ControllerAdvice + @ExceptionHandler 定义全局异常

  1. 创建Spring Boot项目:使用Spring Initializr创建一个新的Spring Boot项目。
  2. 依赖配置:在pom.xml 文件中(方便起见使用的是thymeleaf模板引擎):
<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency>
</dependencies>
  1. HTML目录结构:在src/main/resources/templates 目录下创建HTML视图。这里提供两个示例视图,error-page.htmlresult-page.html

error-page.html

<!DOCTYPE html>
<html xmlns:th="">
<head><title>错误页面</title>
</head>
<body>
<h1>错误</h1>
<p th:text="${message}"></p>
</body>
</html>

result-page.html

<!DOCTYPE html>
<html xmlns:th="">
<head><title>结果页面</title>
</head>
<body>
<h1>结果</h1>
<p th:text="'结果是:' + ${result}"></p>
</body>
</html>
  1. 定义自定义异常类:创建一个自定义异常类,例如 MyCustomException
public class MyCustomException extends Exception {public MyCustomException(String message) {super(message);}
}
  1. 异常处理器类:创建一个异常处理器类 MyCustomExceptionHandler,并配置多个@ExceptionHandler 方法来处理不同类型的异常:
import com.lfsun.demolfsunstudythymeleafcustomexception.exception.MyCustomException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;@ControllerAdvice
public class MyCustomExceptionHandler {@ExceptionHandler(MyCustomException.class)public ModelAndView handleCustomException(MyCustomException ex) {ModelAndView modelAndView = new ModelAndView();modelAndView.addObject("message", "自定义异常发生了: " + ex.getMessage());modelAndView.setViewName("error-page");return modelAndView;}@ExceptionHandler(Exception.class)public ModelAndView handleAllOtherExceptions(Exception ex) {ModelAndView modelAndView = new ModelAndView();modelAndView.addObject("message", "发生了其他异常: " + ex.getMessage());modelAndView.setViewName("error-page");return modelAndView;}
}
  1. 控制器类:创建一个控制器类,例如 DemoController,并在其中抛出自定义异常:
import com.lfsun.demolfsunstudythymeleafcustomexception.exception.MyCustomException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;@Controller
public class DemoController {@GetMapping("/divide")public String divide(@RequestParam int dividend, @RequestParam int divisor, Model model) throws MyCustomException {if (divisor == 0) {throw new MyCustomException("不允许除以零。");}int result = dividend / divisor;model.addAttribute("result", result);return "result-page";}
}
  1. 运行项目:运行Spring Boot应用程序。
  2. 访问应用程序:在浏览器中访问 http://localhost:8080/divide?dividend=10&divisor=2 这个URL,将会执行 DemoController 中的 divide 方法,并显示结果5。尝试访问 http://localhost:8080/divide?dividend=10&divisor=0 来看到自定义异常处理器的效果。

更多推荐

@ControllerAdvice + @ExceptionHandler 定义全局异常

本文发布于:2023-11-15 21:29:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1606640.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:全局   异常   定义   ControllerAdvice   ExceptionHandler

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!