最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

局部異常

局部異常:

在控制器類中添加一個(gè)方法,結(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方法時(shí),會出現(xiàn)除0異常,就會走異常處理方法,封裝異常信息,返回,錯(cuò)誤類封裝的狀態(tài)信息。

全局異常

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

編寫全局異常處理器:

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

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

測試結(jié)果如下:

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

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

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

相關(guān)文章

  • SpringBoot 中使用JSP的方法示例

    SpringBoot 中使用JSP的方法示例

    本篇文章主要介紹了SpringBoot 中使用JSP的方法示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-06-06
  • 詳解springmvc控制登錄用戶session失效后跳轉(zhuǎn)登錄頁面

    詳解springmvc控制登錄用戶session失效后跳轉(zhuǎn)登錄頁面

    本篇文章主要介紹了springmvc控制登錄用戶session失效后跳轉(zhuǎn)登錄頁面,session一旦失效就需要重新登陸,有興趣的同學(xué)可以了解一下。
    2017-01-01
  • 詳解在Java的Struts2框架中配置Action的方法

    詳解在Java的Struts2框架中配置Action的方法

    這篇文章主要介紹了詳解在Java的Struts2框架中配置Action的方法,講解了包括struts.xml中的action配置及基于注解方式Action配置的兩個(gè)方式,需要的朋友可以參考下
    2016-03-03
  • java Volatile與Synchronized的區(qū)別

    java Volatile與Synchronized的區(qū)別

    這篇文章主要介紹了java Volatile與Synchronized的區(qū)別,幫助大家更好的理解和使用Java,感興趣的朋友可以了解下
    2020-12-12
  • Mybatis參數(shù)傳遞示例代碼

    Mybatis參數(shù)傳遞示例代碼

    這篇文章主要給大家介紹了關(guān)于Mybatis參數(shù)傳遞的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-08-08
  • 基于maven install 沒反應(yīng)的解決方法

    基于maven install 沒反應(yīng)的解決方法

    下面小編就為大家?guī)硪黄趍aven install 沒反應(yīng)的解決方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-06-06
  • java通過信號量實(shí)現(xiàn)限流的示例

    java通過信號量實(shí)現(xiàn)限流的示例

    本文主要介紹了java通過信號量實(shí)現(xiàn)限流的示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-06-06
  • Mybatis中的高級映射一對一、一對多、多對多

    Mybatis中的高級映射一對一、一對多、多對多

    這篇文章主要介紹了Mybatis中的高級映射一對一、一對多、多對多的相關(guān)資料,需要的朋友可以參考下
    2016-08-08
  • 在SpringBoot項(xiàng)目中獲取Request的四種方法

    在SpringBoot項(xiàng)目中獲取Request的四種方法

    這篇文章主要為大家詳細(xì)介紹了SpringBoot項(xiàng)目中獲取Request的四種方法,文中的示例代碼講解詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴可以學(xué)習(xí)一下
    2023-11-11
  • SpringBoot集成P6Spy實(shí)現(xiàn)SQL日志的記錄詳解

    SpringBoot集成P6Spy實(shí)現(xiàn)SQL日志的記錄詳解

    P6Spy是一個(gè)框架,它可以無縫地?cái)r截和記錄數(shù)據(jù)庫活動,而無需更改現(xiàn)有應(yīng)用程序的代碼。一般我們使用的比較多的是使用p6spy打印我們最后執(zhí)行的sql語句
    2022-11-11

最新評論

江都市| 阳原县| 丰顺县| 日照市| 横峰县| 汾阳市| 邢台县| 论坛| 依兰县| 肇州县| 新巴尔虎右旗| 金华市| 达拉特旗| 灵石县| 高雄市| 安新县| 玛多县| 广昌县| 大兴区| 陆丰市| 嘉义县| 察隅县| 金昌市| 仙游县| 陇川县| 松滋市| 滨州市| 新乐市| 巴东县| 额尔古纳市| 杭州市| 钟祥市| 麻栗坡县| 黑山县| 唐河县| 扶风县| 龙泉市| 赤水市| 夏河县| 双桥区| 安庆市|