Java SpringMVC異常處理機(jī)制詳解
異常處理的思路

測試環(huán)境準(zhǔn)備
首先寫一個(gè)DemoController控制層的類作為測試訪問的控制器
package com.itheima.controller;
import com.itheima.exception.MyException;
import com.itheima.service.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.io.FileNotFoundException;
@Controller
public class DemoController {
@Autowired
private DemoService demoService;
@RequestMapping(value="/show")
public String show() throws FileNotFoundException, MyException {
System.out.println("show running");
demoService.show1();
// demoService.show2();
// demoService.show3();
// demoService.show4();
// demoService.show5();
return "index";
}
}
然后在service中寫上接口DemoService和實(shí)現(xiàn)類DemoServiceImpl
package com.itheima.service;
import com.itheima.exception.MyException;
import java.io.FileNotFoundException;
public interface DemoService {
public void show1();
public void show2();
public void show3() throws FileNotFoundException;
public void show4();
public void show5() throws MyException;
}
package com.itheima.service.impl;
import com.itheima.exception.MyException;
import com.itheima.service.DemoService;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
public class DemoServiceImpl implements DemoService {
public void show1() {
System.out.println("類型轉(zhuǎn)換異常");
Object str = "zhangsan";
Integer num = (Integer) str;
}
public void show2() {
System.out.println("拋出除零異常");
int i = 1 / 0;
}
public void show3() throws FileNotFoundException {
System.out.println("文件找不到異常");
InputStream in = new FileInputStream("C:/xxx/xxx/xxx.txt");
}
public void show4() {
System.out.println("空指針異常");
String str = null;
str.length();
}
public void show5() throws MyException {
System.out.println("自定義異常");
throw new MyException();
}
}
其中的MyException是自定義異常,被聲明在itheima的另一個(gè)包下,此時(shí)還未任何實(shí)現(xiàn):

訪問一下/show,因?yàn)橄日{(diào)用的show1方法,所以會報(bào)類型轉(zhuǎn)換異常:

環(huán)境準(zhǔn)備完畢。
異常處理兩種方式

方式一:簡單異常處理器

方式一很簡單,去做對應(yīng)的配置文件配置就可以了:
<!--配置異常處理器-->
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<!--如果下面全都對不上,就會采用這個(gè)默認(rèn)的defaultErrorView所對應(yīng)的value值的視圖-->
<property name="defaultErrorView" value="error"></property>
<property name="exceptionMappings">
<map>
<!--類型轉(zhuǎn)換異常,拋了什么異常,就會跳轉(zhuǎn)到value值對應(yīng)的字符串顯示的頁面-->
<entry key="java.lang.ClassCastException" value="error"></entry>
<!--自定義異常-->
<entry key="com.itheima.exception.MyException" value="error"></entry>
</map>
</property>
</bean>
然后再進(jìn)行訪問,可以看到跳轉(zhuǎn)到了error視圖:

方式二:自定義異常處理器
步驟;
1、創(chuàng)建異常處理器類實(shí)現(xiàn)HandlerExceptionResolver
2、配置異常處理器
3、編寫異常頁面
4、測試異常跳轉(zhuǎn)
演示;
第一步:創(chuàng)建異常處理器類實(shí)現(xiàn)HandlerExceptionResolver
package com.itheima.resolver;
import com.itheima.exception.MyException;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MyExceptionResolver implements HandlerExceptionResolver {
/*這是HandlerExceptionResolver中必須要實(shí)現(xiàn)的方法
參數(shù)Exception:異常對象
返回值ModelAndView:跳轉(zhuǎn)到錯(cuò)誤視圖信息
*/
public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
ModelAndView md = new ModelAndView();
/*
這里只是大致做一個(gè)代碼邏輯的演示
實(shí)際開發(fā)當(dāng)中并不會這樣寫,沒什么意義
下面的演示只是可以告訴我們可以在這個(gè)方法里面進(jìn)行異常信息的判斷
*/
if(e instanceof MyException){
md.addObject("info","自定義異常");
}else if(e instanceof ClassCastException){
md.addObject("info","類型轉(zhuǎn)換異常");
}
md.setViewName("error");
return md;
}
}
第二步:在SpringMVC的配置文件當(dāng)中配置異常處理器
<!--自定義異常處理器-->
<bean class="com.itheima.resolver.MyExceptionResolver"></bean>
測試訪問就行了。
總結(jié)

本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
SpringBoot如何進(jìn)行參數(shù)校驗(yàn)實(shí)例詳解
開發(fā)過程中,后臺的參數(shù)校驗(yàn)是必不可少的,下面這篇文章主要給大家介紹了關(guān)于SpringBoot如何進(jìn)行參數(shù)校驗(yàn)的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-01-01
Java基于ShardingSphere實(shí)現(xiàn)分庫分表的實(shí)例詳解
ShardingSphere?已于2020年4月16日成為?Apache?軟件基金會的頂級項(xiàng)目,?它們均提供標(biāo)準(zhǔn)化的數(shù)據(jù)水平擴(kuò)展、分布式事務(wù)和分布式治理等功能,可適用于如?Java?同構(gòu)、異構(gòu)語言、云原生等各種多樣化的應(yīng)用場景,對ShardingSphere分庫分表相關(guān)知識感興趣的朋友一起看看吧2022-03-03
SpringBoot全局配置long轉(zhuǎn)String丟失精度問題解決方案
這篇文章主要介紹了SpringBoot全局配置long轉(zhuǎn)String丟失精度問題解決方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08
RabbitMQ消息隊(duì)列中的Channel信道參數(shù)詳解
這篇文章主要介紹了RabbitMQ消息隊(duì)列中的Channel信道參數(shù)詳解,信道是生產(chǎn)消費(fèi)者與rabbit通信的渠道,生產(chǎn)者publish或者消費(fèi)者消費(fèi)一個(gè)隊(duì)列都是需要通過信道來通信的,信道是建立在TCP上面的虛擬鏈接,需要的朋友可以參考下2023-08-08
關(guān)于Idea創(chuàng)建Java項(xiàng)目并引入lombok包的問題(lombok.jar包免費(fèi)下載)
很多朋友遇到當(dāng)idea創(chuàng)建java項(xiàng)目時(shí),命名安裝了lombok插件卻不能使用注解,原因有兩個(gè)大家可以參考下本文,本文對每種原因分析給出了解決方案,需要的朋友參考下吧2021-06-06

