spring boot配置統一異常處理

2021-09-24 00:08:52 字數 2588 閱讀 9714

基於@controlleradvice的統一異常處理

>.這裡serverexception是我自定義的異常,和普通exception分開處理

>.這裡的requestresult是我自定義的請求返回結果物件

import

com.dawn.blogspot.common.exception.serverexception;

import

com.dawn.blogspot.common.response.requestresult;

import

org.slf4j.logger;

import

org.slf4j.logge***ctory;

import

org.springframework.web.bind.annotation.controlleradvice;

import

org.springframework.web.bind.annotation.exceptionhandler;

import

org.springframework.web.bind.annotation.responsebody;

/***

@author

tangzedong

* @apinote 統一異常處理器

* @since

2018/5/21 11:55 */

@controlleradvice

public

class

exceptionresolver ,並返回錯誤資訊

*/@responsebody

@exceptionhandler(value = serverexception.class

)

public

requestresult serverexception(serverexception e)

/*** 處理自定義異常,並返回錯誤資訊

*/@responsebody

@exceptionhandler(value = exception.class

)

public

requestresult serverexception()

}

/**

* @author

tangzedong

* @apinote 服務異常

* @since

2018/9/4 15:27 */

public

class serverexception extends

runtimeexception

public

serverexception(string message)

public serverexception(short

code, string message)

public serverexception(short

code, string message, throwable t)

}

/**

* @author

tangzedong

* @apinote 請求結果

* @since

2018/9/4 16:20 */

public

class requestresult

//get方法

public

short

getcode()

public

string getmessage()

public

t getdata()

public

boolean

issuccess()

//獲取例項

public

static

requestresult getfailedinstance(short

code, string message, t data)

public

static

requestresult getfailedinstance(short

code, string message)

public

static

requestresult getfailedinstance(string message)

public

static

requestresult getsuccessinstance(short

code, string message, t data)

public

static

requestresult getsuccessinstance(short

code, t data)

public

static

requestresult getsuccessinstance(t data)

}

posted @

2018-09-04 17:13

dawn-tangzedong 閱讀(

...)

編輯收藏

Spring Boot中Web應用的統一異常處理

我們在做web應用的時候,請求處理過程中發生錯誤是非常常見的情況。spring boot提供了乙個預設的對映 error,當處理中丟擲異常之後,會轉到該請求中處理,並且該請求有乙個全域性的錯誤頁面用來展示異常內容。如圖 雖然,spring boot中實現了預設的error對映,但是在實際應用中,上面...

Spring Boot中Web應用的統一異常處理

我們在做web應用的時候,請求處理過程中發生錯誤是非常常見的情況。spring boot提供了乙個預設的對映 error,當處理中丟擲異常之後,會轉到該請求中處理,並且該請求有乙個全域性的錯誤頁面用來展示異常內容。選擇乙個之前實現過的web應用 chapter3 1 2 為基礎,啟動該應用,訪問乙個...

Spring Boot中Web應用的統一異常處理

我們在做web應用的時候,請求處理過程中發生錯誤是非常常見的情況。spring boot提供了乙個預設的對映 error,當處理中丟擲異常之後,會轉到該請求中處理,並且該請求有乙個全域性的錯誤頁面用來展示異常內容。選擇乙個之前實現過的web應用 chapter3 1 2 為基礎,啟動該應用,訪問乙個...