日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

018_SpringBoot异常处理方式-ExceptionHandle注解处理异常

發布時間:2025/5/22 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 018_SpringBoot异常处理方式-ExceptionHandle注解处理异常 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1. 使用maven構建SpringBoot的名叫spring-boot-exception2項目

2. pom.xml?

<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"><modelVersion>4.0.0</modelVersion><groupId>com.bjbs</groupId><artifactId>spring-boot-exception2</artifactId><version>0.0.1-SNAPSHOT</version><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.13.RELEASE</version></parent><!-- 修改jdk版本 --><properties><java.version>1.8</java.version><!-- 指定thymeleaf和thymeleaf-layout-dialect高版本可以防止html標簽不規范報錯 --><thymeleaf.version>3.0.2.RELEASE</thymeleaf.version><thymeleaf-layout-dialect.version>2.0.4</thymeleaf-layout-dialect.version></properties><dependencies><!-- springBoot的啟動器 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency></dependencies> </project>

3. 新建UserController.java

package com.bjbs.controller;import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView;@Controller public class UserController {@RequestMapping("/nullException")public String nullException(){String str = null;str.length();return "index";}@RequestMapping("/byZeroException") // java.lang.ArithmeticExceptionpublic String byZeroException(){int a = 10/0;return "index";}/*** 發生該java.lang.NullPointerException異常走這里* 該方法需要返回一個ModelAndView: 目的是可以讓我們封裝異常信息以及視圖的指定* 參數Exception e: 會將產生異常對象注入到方法中*/@ExceptionHandler(value={java.lang.NullPointerException.class})public ModelAndView nullPointerExceptionHandler(Exception e){ModelAndView mv = new ModelAndView();mv.addObject("error", "自定義空指針錯誤提示信息: " + e.toString());mv.setViewName("error");return mv;}/*** 發生該java.lang.ArithmeticException異常走這里* 該方法需要返回一個ModelAndView: 目的是可以讓我們封裝異常信息以及視圖的指定* 參數Exception e: 會將產生異常對象注入到方法中*/@ExceptionHandler(value={java.lang.ArithmeticException.class})public ModelAndView arithmeticExceptionHandler(Exception e){ModelAndView mv = new ModelAndView();mv.addObject("error", "自定義除以0錯誤提示信息: " + e.toString());mv.setViewName("error");return mv;} }

4. 新建App.java

package com.bjbs;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;/*** SpringBoot啟動類*/ @SpringBootApplication public class App {public static void main(String[] args) {SpringApplication.run(App.class, args);} }

5. 在src/main/resources/templates下, 新建index.html

<!DOCTYPE html> <html><head><meta charset="UTF-8" /><title>主頁</title></head><body><span th:text="Hello"></span></body> </html>

6. 在src/main/resources/templates下, 新建error.html, 注意: 名稱必須叫error。

<!DOCTYPE html> <html><head><meta charset="UTF-8" /><title>錯誤提示頁面</title></head><body>出錯了, 請與管理員聯系。<span th:text="${error}"></span></body> </html>

7. 運行項目, 使用瀏覽器訪問空指針異常

8. 運行項目, 使用瀏覽器訪問除數為0異常?

《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀

總結

以上是生活随笔為你收集整理的018_SpringBoot异常处理方式-ExceptionHandle注解处理异常的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。