spring-boot异常处理

迷南。 2024-02-19 21:57 189阅读 0赞

Emm昨天台风停电就没写博客

Spring-boot异常处理
如果你在src/main/resource/templates目录下新建了error.html。那么当服务器出现异常的时候都会跳转到error.html。但是这样的方法,不适合我们开发
讲下另外一种方法
1.配置依pom.xml

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  2. <modelVersion>4.0.0</modelVersion>
  3. <parent>
  4. <groupId>org.springframework.boot</groupId>
  5. <artifactId>spring-boot-starter-parent</artifactId>
  6. <version>1.5.10.RELEASE</version>
  7. </parent>
  8. <groupId>com.bjsxt</groupId>
  9. <artifactId>18-spring-boot-exception5</artifactId>
  10. <version>0.0.1-SNAPSHOT</version>
  11. <properties>
  12. <java.version>1.7</java.version>
  13. <thymeleaf.version>3.0.2.RELEASE</thymeleaf.version>
  14. <thymeleaf-layout-dialect.version>2.0.4</thymeleaf-layout-dialect.version>
  15. </properties>
  16. <dependencies>
  17. <!-- springBoot的启动器 -->
  18. <dependency>
  19. <groupId>org.springframework.boot</groupId>
  20. <artifactId>spring-boot-starter-web</artifactId>
  21. </dependency>
  22. <!-- springBoot的启动器 -->
  23. <dependency>
  24. <groupId>org.springframework.boot</groupId>
  25. <artifactId>spring-boot-starter-thymeleaf</artifactId>
  26. </dependency>
  27. </dependencies>
  28. </project>

2.在启动器的子目录下新建Exception类 ,类实现HandlerExceptionResolver接口。重写resolveException方法

  1. package cn.zds.ecception;
  2. import javax.servlet.http.HttpServletRequest;
  3. import javax.servlet.http.HttpServletResponse;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6. import org.springframework.web.bind.annotation.ControllerAdvice;
  7. import org.springframework.web.bind.annotation.ExceptionHandler;
  8. import org.springframework.web.servlet.HandlerExceptionResolver;
  9. import org.springframework.web.servlet.ModelAndView;
  10. import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver;
  11. /**
  12. * 通过实现HandlerExceptionResolver接口做全局异常处理
  13. *
  14. *
  15. */
  16. @Configuration//标记异常处理类
  17. public class GlobalException implements HandlerExceptionResolver {
  18. @Override
  19. public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler,
  20. Exception ex) {
  21. ModelAndView mv = new ModelAndView();
  22. //判断不同异常类型,做不同视图跳转
  23. if(ex instanceof ArithmeticException){//如果是算术异常
  24. mv.setViewName("error1");//设置ModelAndView的跳转路径视图(error1.html)
  25. }
  26. if(ex instanceof NullPointerException){//空指针异常
  27. mv.setViewName("error2");设置ModelAndView的跳转路径视图(error2.html
  28. }
  29. mv.addObject("error", ex.toString());//将异常对象添加到ModelAndView
  30. return mv;
  31. }
  32. }

3.在视图中可输出异常信息

error1.html

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Insert title here</title>
  6. </head>
  7. <body>
  8. 出错了 1
  9. <span th:text="${error}"></span>
  10. </body>
  11. </html>

error2.html

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Insert title here</title>
  6. </head>
  7. <body>
  8. 出错了 2
  9. <span th:text="${error}"></span>
  10. </body>
  11. </html>

4.启动器App.java:

  1. package cn.zds;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. /**
  5. *
  6. *SpringBoot处理异常方式一:自定义错误页面
  7. *
  8. */
  9. @SpringBootApplication
  10. public class App {
  11. public static void main(String[] args) {
  12. SpringApplication.run(App.class, args);
  13. }
  14. }

5.测试:
编写controller

  1. package cn.zds.controller;
  2. import org.springframework.stereotype.Controller;
  3. import org.springframework.web.bind.annotation.RequestMapping;
  4. @Controller
  5. public class ExceptionController {
  6. //除0错误
  7. @RequestMapping("error1")
  8. public String error1(){
  9. int a=10/0;
  10. return "add";
  11. }
  12. //空指针错误
  13. @RequestMapping("error2")
  14. public String error2(){
  15. String string=null;
  16. string.contains("aa");
  17. return "add";
  18. }
  19. }

6.项目结构:
在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,189人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Springboot 全局异常处理

    最近在做项目时需要对异常进行全局统一处理,主要是一些分类入库以及记录日志等,因为项目是基于Springboot的,所以去网络上找了一些博客文档,然后再结合项目本身的一些特殊需求