SpringBoot業(yè)務(wù)邏輯異常的處理方法介紹
在Spring Boot項(xiàng)目中除了設(shè)置錯(cuò)誤頁(yè)面,還可以通過(guò)注解實(shí)現(xiàn)錯(cuò)誤處理。
局部異常
局部異常:
在控制器類中添加一個(gè)方法,結(jié)合@ExceptionHandler。但是只能對(duì)當(dāng)前控制器中方法出現(xiàn)異常進(jìn)行解決。
引入lombok依賴
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
1.創(chuàng)建異常信息類
package com.yyl.firstdemo.exception;
import lombok.Data;
@Data
public class ExceptionMessage {
private String code;
private String message;
}2.在controller中設(shè)置異常處理
package com.yyl.firstdemo.controller;
import com.yyl.firstdemo.exception.ExceptionMessage;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class ErrorController {
@RequestMapping("/test")
public String testError(){
System.out.println(5/0);
return "500";
}
@ExceptionHandler(ArithmeticException.class)
@ResponseBody
public ExceptionMessage arithmeticException(Exception e){
ExceptionMessage exceptionMessage = new ExceptionMessage();
exceptionMessage.setCode("500");
exceptionMessage.setMessage(e.getMessage());
return exceptionMessage;
}
}@ExceptionHandler的參數(shù)為發(fā)生異常的類型。如果controller的方法中捕獲到了這種異常,就會(huì)走@ExceptionHandler表示的方法arithmeticException(),在方法參數(shù)中,可以獲取異常對(duì)象。
最終執(zhí)行結(jié)果:

當(dāng)訪問(wèn)test的controller方法時(shí),會(huì)出現(xiàn)除0異常,就會(huì)走異常處理方法,封裝異常信息,返回,錯(cuò)誤類封裝的狀態(tài)信息。
全局異常
新建全局異常類,通過(guò)@ControllerAdvice結(jié)合@ExceptionHandler。當(dāng)全局異常處理和局部處理同時(shí)存在時(shí),局部生效(就近原則)
編寫(xiě)全局異常處理器:
package com.yyl.firstdemo.exception;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
@ControllerAdvice
public class GlobalExceptionHandleController {
//發(fā)生除0異常時(shí),會(huì)執(zhí)行該方法
@ExceptionHandler(ArithmeticException.class)
@ResponseBody
public ExceptionMessage arithmeticException(Exception e){
ExceptionMessage exceptionMessage = new ExceptionMessage();
exceptionMessage.setCode("500");
exceptionMessage.setMessage(e.getMessage());
return exceptionMessage;
}
//發(fā)生空指針異常時(shí)會(huì)執(zhí)行該方法
@ExceptionHandler(NullPointerException.class)
@ResponseBody
public ExceptionMessage nullPointerException(NullPointerException
e){
ExceptionMessage exceptionMessage = new ExceptionMessage();
exceptionMessage.setCode("500");
exceptionMessage.setMessage(e.getMessage());
return exceptionMessage;
}
//發(fā)生其他未知異常時(shí),會(huì)執(zhí)行該方法
@ExceptionHandler(Exception.class)
@ResponseBody
public ExceptionMessage otherException(){
ExceptionMessage exceptionMessage = new ExceptionMessage();
exceptionMessage.setCode("500");
exceptionMessage.setMessage("發(fā)生了其他未知異常!");
return exceptionMessage;
}
}編寫(xiě)controller,增加兩個(gè)方法:
package com.yyl.firstdemo.controller;
import com.yyl.firstdemo.exception.ExceptionMessage;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class ErrorController {
@RequestMapping("/test")
public String testError(){
System.out.println(5/0);
return "500";
}
@RequestMapping("/testNull")
public String testNull(){
String s = null;
System.out.println(s.length());
return null;
}
@RequestMapping("testOther")
public String testOther(){
int[] arr = new int[3];
System.out.println(arr[5]);
return null;
}
}測(cè)試結(jié)果如下:



我們?cè)偌由暇植慨惓?,再進(jìn)行測(cè)試:
我們發(fā)現(xiàn)就近原則生效,異常被局部異常處理器捕獲處理

到此這篇關(guān)于SpringBoot業(yè)務(wù)邏輯異常的處理方法介紹的文章就介紹到這了,更多相關(guān)SpringBoot業(yè)務(wù)邏輯異常內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解SpringBoot上傳圖片到阿里云的OSS對(duì)象存儲(chǔ)中
這篇文章主要介紹了SpringBoot上傳圖片到阿里云的OSS對(duì)象存儲(chǔ)中,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10
Spring+Quartz配置定時(shí)任務(wù)實(shí)現(xiàn)代碼
這篇文章主要介紹了Spring+Quartz配置定時(shí)任務(wù)實(shí)現(xiàn)代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04
淺談Spring AOP中args()和argNames的含義
這篇文章主要介紹了Spring AOP中args()和argNames的含義,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
詳解Java豆瓣電影爬蟲(chóng)——小爬蟲(chóng)成長(zhǎng)記(附源碼)
這篇文章主要介紹了詳解Java豆瓣電影爬蟲(chóng)——小爬蟲(chóng)成長(zhǎng)記(附源碼) ,具有一定的參考價(jià)值,有需要的可以了解一下。2016-12-12
Java List的remove()方法陷阱以及性能優(yōu)化
Java List在進(jìn)行remove()方法是通常容易踩坑,本文就詳細(xì)的介紹一下陷阱以及性能優(yōu)化,感興趣的可以了解一下2021-10-10
Java 信號(hào)量Semaphore的實(shí)現(xiàn)
這篇文章主要介紹了Java 信號(hào)量Semaphore的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
BUUCTF-easy java WEB-INF/web.xml泄露漏洞及其利用方式
這篇文章主要介紹了BUUCTF-easy java WEB-INF/web.xml泄露漏洞及其利用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07
springboot?ElasticSearch如何配置自定義轉(zhuǎn)換器ElasticsearchCustomConver
這篇文章主要介紹了springboot?ElasticSearch如何配置自定義轉(zhuǎn)換器ElasticsearchCustomConversions問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08

