日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > java >内容正文

java

JavaWeb学习之路——SpringBoot 中几种异常处理方法(四)

發(fā)布時(shí)間:2024/9/30 java 17 豆豆
生活随笔 收集整理的這篇文章主要介紹了 JavaWeb学习之路——SpringBoot 中几种异常处理方法(四) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Spring?Boot(三)

錯(cuò)誤處理

1.自定義處理異常,返回指定界面

controller出現(xiàn)錯(cuò)誤的方法中:

@RequestMapping("/error")public String error() {String str=null;str.length();return "test";}

自定義錯(cuò)誤頁面.html中:

<!DOCTYPE html><html xmlns:th="http://www.thymeleaf.org"><head><meta charset="UTF-8"><title>Title</title></head><body>出錯(cuò)了<span th:text="${error}"></span></body></html>

?

2.捕獲異常,ExceptionHandler?處理方法

方法中出現(xiàn)異常

@RequestMapping("/error")public String error() {String str=null;str.length();return "test";}

1)Handler處理器ExceptionHandler捕獲指定異常

@ExceptionHandler(value = {java.lang.NullPointerException.class})public ModelAndView excuteException(Exception e){ModelAndView mv=new ModelAndView();mv.addObject("error",e.toString());mv.setViewName("error1");return mv;}

?

2)跳轉(zhuǎn)至error1視圖

<!DOCTYPE html><html xmlns:th="http://www.thymeleaf.org"><head><meta charset="UTF-8"><title>Title</title></head><body>error1界面<span th:text="${error}"></span></body></html>

?

3.全局異常處理——SimpleMappingExceptionResolver類

1)通過創(chuàng)建一個(gè)類,添加@Configuration+@Bean注解方式可以讓springboot啟動(dòng)時(shí)注冊(cè)這個(gè)bean

SpringBoot啟動(dòng)時(shí)加載這個(gè)bean,發(fā)生指定錯(cuò)誤返回指定界面

@Configurationpublic class GlobalException {@Beanpublic SimpleMappingExceptionResolver getExceptionResolver(){SimpleMappingExceptionResolver resolver=new SimpleMappingExceptionResolver();Properties mappings=new Properties();/** 參數(shù)一:異常的類型,異常類型的全名* 參數(shù)二:視圖名稱* */mappings.put("java.lang.NullPointerException","error1");resolver.setExceptionMappings(mappings);return resolver;}}

?

2)出現(xiàn)錯(cuò)誤方法

@RequestMapping("/error")public String error() {String str=null;str.length();return "test";}

?

4.自定義異常類——HandlerExceptionResolver

1)實(shí)現(xiàn)HandlerExceptionResolver接口里面的方法

@Configurationpublic class GlobalException implements HandlerExceptionResolver {/*對(duì)resolveException對(duì)象進(jìn)行錯(cuò)誤類型判斷*/@Overridepublic ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {ModelAndView mv=new ModelAndView();if (e instanceof NullPointerException){System.out.println("java.lang.NullPointerException");mv.setViewName("test");}mv.addObject("error",e.toString());return mv;}}

2)Controller里面出現(xiàn)異常的方法

@RequestMapping("/error")public String error() {String str=null;str.length();return "test";}

3)出現(xiàn)指定錯(cuò)誤,返回指定視圖html

test.html視圖

<!DOCTYPE html><html xmlns:th="http://www.thymeleaf.org"><head><meta charset="UTF-8"><title>Title</title></head><body>出錯(cuò)了<span th:text="${error}"></span></body></html>

?

總結(jié)

以上是生活随笔為你收集整理的JavaWeb学习之路——SpringBoot 中几种异常处理方法(四)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。