欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot業(yè)務(wù)邏輯異常的處理方法介紹

 更新時間:2022年09月13日 10:22:18   作者:流楚丶格念  
本篇文章為大家展示了如何在SpringBoot中統(tǒng)一處理邏輯異常,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲

在Spring Boot項(xiàng)目中除了設(shè)置錯誤頁面,還可以通過注解實(shí)現(xiàn)錯誤處理。

局部異常

局部異常:

在控制器類中添加一個方法,結(jié)合@ExceptionHandler。但是只能對當(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的方法中捕獲到了這種異常,就會走@ExceptionHandler表示的方法arithmeticException(),在方法參數(shù)中,可以獲取異常對象。

最終執(zhí)行結(jié)果:

當(dāng)訪問test的controller方法時,會出現(xiàn)除0異常,就會走異常處理方法,封裝異常信息,返回,錯誤類封裝的狀態(tài)信息。

全局異常

新建全局異常類,通過@ControllerAdvice結(jié)合@ExceptionHandler。當(dāng)全局異常處理和局部處理同時存在時,局部生效(就近原則)

編寫全局異常處理器:

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異常時,會執(zhí)行該方法
    @ExceptionHandler(ArithmeticException.class)
    @ResponseBody
    public ExceptionMessage arithmeticException(Exception e){
        ExceptionMessage exceptionMessage = new ExceptionMessage();
        exceptionMessage.setCode("500");
        exceptionMessage.setMessage(e.getMessage());
        return exceptionMessage;
    }
    //發(fā)生空指針異常時會執(zhí)行該方法
    @ExceptionHandler(NullPointerException.class)
    @ResponseBody
    public ExceptionMessage nullPointerException(NullPointerException
                                                         e){
        ExceptionMessage exceptionMessage = new ExceptionMessage();
        exceptionMessage.setCode("500");
        exceptionMessage.setMessage(e.getMessage());
        return exceptionMessage;
    }
    //發(fā)生其他未知異常時,會執(zhí)行該方法
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public ExceptionMessage otherException(){
        ExceptionMessage exceptionMessage = new ExceptionMessage();
        exceptionMessage.setCode("500");
        exceptionMessage.setMessage("發(fā)生了其他未知異常!");
        return exceptionMessage;
    }
}

編寫controller,增加兩個方法:

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;
    }
}

測試結(jié)果如下:

我們再加上局部異常,再進(jìn)行測試:

我們發(fā)現(xiàn)就近原則生效,異常被局部異常處理器捕獲處理

到此這篇關(guān)于SpringBoot業(yè)務(wù)邏輯異常的處理方法介紹的文章就介紹到這了,更多相關(guān)SpringBoot業(yè)務(wù)邏輯異常內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 詳解SpringBoot上傳圖片到阿里云的OSS對象存儲中

    詳解SpringBoot上傳圖片到阿里云的OSS對象存儲中

    這篇文章主要介紹了SpringBoot上傳圖片到阿里云的OSS對象存儲中,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-10-10
  • Spring+Quartz配置定時任務(wù)實(shí)現(xiàn)代碼

    Spring+Quartz配置定時任務(wù)實(shí)現(xiàn)代碼

    這篇文章主要介紹了Spring+Quartz配置定時任務(wù)實(shí)現(xiàn)代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-04-04
  • 淺談Spring AOP中args()和argNames的含義

    淺談Spring AOP中args()和argNames的含義

    這篇文章主要介紹了Spring AOP中args()和argNames的含義,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • SpringCloud Alibaba框架介紹

    SpringCloud Alibaba框架介紹

    spring cloud是一個基于springboot實(shí)現(xiàn)的微服務(wù)架構(gòu)開發(fā)工具,目前主流的SpringCloud分為SpringCloud Netflix和阿里云開源的SpringCloud Alibaba兩個系列,本文主要介紹SpringCloud Alibaba框架,感興趣的朋友可以參考一下
    2023-04-04
  • 詳解Java豆瓣電影爬蟲——小爬蟲成長記(附源碼)

    詳解Java豆瓣電影爬蟲——小爬蟲成長記(附源碼)

    這篇文章主要介紹了詳解Java豆瓣電影爬蟲——小爬蟲成長記(附源碼) ,具有一定的參考價值,有需要的可以了解一下。
    2016-12-12
  • Java List的remove()方法陷阱以及性能優(yōu)化

    Java List的remove()方法陷阱以及性能優(yōu)化

    Java List在進(jìn)行remove()方法是通常容易踩坑,本文就詳細(xì)的介紹一下陷阱以及性能優(yōu)化,感興趣的可以了解一下
    2021-10-10
  • Java 信號量Semaphore的實(shí)現(xiàn)

    Java 信號量Semaphore的實(shí)現(xiàn)

    這篇文章主要介紹了Java 信號量Semaphore的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • BUUCTF-easy java WEB-INF/web.xml泄露漏洞及其利用方式

    BUUCTF-easy java WEB-INF/web.xml泄露漏洞及其利用方式

    這篇文章主要介紹了BUUCTF-easy java WEB-INF/web.xml泄露漏洞及其利用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • DTD驗(yàn)證xml格式的三種方式詳解

    DTD驗(yàn)證xml格式的三種方式詳解

    這篇文章主要介紹了DTD驗(yàn)證xml格式的三種方式詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-10-10
  • springboot?ElasticSearch如何配置自定義轉(zhuǎn)換器ElasticsearchCustomConversions

    springboot?ElasticSearch如何配置自定義轉(zhuǎn)換器ElasticsearchCustomConver

    這篇文章主要介紹了springboot?ElasticSearch如何配置自定義轉(zhuǎn)換器ElasticsearchCustomConversions問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-08-08

最新評論