spring boot 全局異常處理方法匯總
這篇文章主要介紹了spring boot 全局異常處理方法匯總,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
import cn.sisyphe.framework.web.exception.DataException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageConversionException;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
import javax.servlet.http.HttpServletRequest;
/**
* @author ming
* @desc 全局異常處理類
*/
@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {
/**
* 缺失請(qǐng)求參數(shù)處理
*
* @param e
* @param request
* @return
*/
@ExceptionHandler(MissingServletRequestParameterException.class)
@ResponseBody
public ResponseResult handleMissingServletRequestParameterException(MissingServletRequestParameterException e, HttpServletRequest request) {
String message = "缺失請(qǐng)求參數(shù)" + e.getParameterName();
return ackTransfer(request, message, HttpStatus.BAD_REQUEST.value() + "", e);
}
/**
* 請(qǐng)求參數(shù)類型錯(cuò)誤處理
*
* @param e
* @param request
* @return
*/
@ExceptionHandler(MethodArgumentTypeMismatchException.class)
@ResponseBody
public ResponseResult handleMethodArgumentTypeMismatchException(MethodArgumentTypeMismatchException e, HttpServletRequest request) {
String message = "請(qǐng)求參數(shù)" + e.getName() + "類型錯(cuò)誤";
return ackTransfer(request, message, HttpStatus.BAD_REQUEST.value() + "", e);
}
/**
* 參數(shù)類型錯(cuò)誤異常類型處理
*
* @param e
* @param request
* @return
*/
@ExceptionHandler(HttpMessageConversionException.class)
@ResponseBody
public ResponseResult handleHttpMessageNotReadableException(HttpMessageConversionException e, HttpServletRequest request) {
String message = "參數(shù)類型錯(cuò)誤";
return ackTransfer(request, message, HttpStatus.BAD_REQUEST.value() + "", e);
}
/**
* 空指針異常處理
*
* @param e
* @param request
* @return
*/
@ExceptionHandler(NullPointerException.class)
@ResponseBody
public ResponseResult handleNullPointerException(NullPointerException e, HttpServletRequest request) {
String message = "空指針異常";
return ackTransfer(request, message, HttpStatus.BAD_REQUEST.value() + "", e, true);
}
/**
* MethodArgumentNotValidException 異常處理
* @param e
* @param request
* @return
*/
@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseBody
public ResponseResult handleMethodArgumentNotValidException(MethodArgumentNotValidException e, HttpServletRequest request) {
StringBuilder errorMsg = new StringBuilder();
BindingResult re = e.getBindingResult();
for (ObjectError error : re.getAllErrors()) {
errorMsg.append(error.getDefaultMessage()).append(",");
}
errorMsg.delete(errorMsg.length() - 1, errorMsg.length());
return ackTransfer(request, errorMsg.toString(), "-1", e, false);
}
/**
* 綁定異常處理
* @param e
* @param request
* @return
*/
@ExceptionHandler(BindException.class)
@ResponseBody
public ResponseResult handleBindException(BindException e,HttpServletRequest request){
BindingResult result = e.getBindingResult();
StringBuilder errorMsg = new StringBuilder();
for (ObjectError error : result.getAllErrors()) {
errorMsg.append(error.getDefaultMessage()).append(",");
}
errorMsg.delete(errorMsg.length() - 1, errorMsg.length());
return ackTransfer(request, errorMsg.toString(), "-1", e, false);
}
/**
* 自定義異常類型異常消息處理
*
* @param e
* @param request
* @return
*/
@ExceptionHandler({DataException.class})
@ResponseBody
public ResponseResult handleDataException(DataException e, HttpServletRequest request) {
String message = e.getErrorMessage();
String code = e.getErrorCode();
return ackTransfer(request, code, message, e, true);
}
/**
* 處理運(yùn)行時(shí)異常
*
* @param e
* @param request
* @return
*/
@ExceptionHandler({RuntimeException.class})
@ResponseBody
public ResponseResult handleRuntimeException(RuntimeException e, HttpServletRequest request) {
return ackTransfer(request, e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR.value() + "", e, true);
}
/**
* 默認(rèn)異常處理
*
* @param e
* @param request
* @return
*/
@ExceptionHandler(Exception.class)
@ResponseBody
public ResponseResult handleException(Exception e, HttpServletRequest request) {
return ackTransfer(request, e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR.value() + "", e, true);
}
private ResponseResult ackTransfer(HttpServletRequest request, String message, String code, Exception e, boolean printStackTrace) {
ResponseResult result = new ResponseResult();
result.setCode(code);
result.setMessage(message);
if (printStackTrace) {
log.error(message, e);
} else {
log.error(message);
}
return result;
}
private ResponseResult ackTransfer(HttpServletRequest request, String message, String code, Exception e) {
return ackTransfer(request, message, code, e, false);
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- java將String字符串轉(zhuǎn)換為L(zhǎng)ist<Long>類型實(shí)例方法
- SpringBoot新特性之全局懶加載機(jī)制
- SpringBoot如何讀取配置文件參數(shù)并全局使用
- spring boot教程之全局處理異常封裝
- Springboot之自定義全局異常處理的實(shí)現(xiàn)
- Spring Boot處理全局統(tǒng)一異常的兩種方法與區(qū)別
- SpringBoot如何優(yōu)雅的處理全局異常
- springboot結(jié)合全局異常處理實(shí)現(xiàn)登錄注冊(cè)驗(yàn)證
- SpringBoot全局配置long轉(zhuǎn)String丟失精度問(wèn)題解決方案
相關(guān)文章
springboot中request和response的加解密實(shí)現(xiàn)代碼
這篇文章主要介紹了springboot中request和response的加解密實(shí)現(xiàn),在springboot中提供了RequestBodyAdviceAdapter和ResponseBodyAdvice,利用這兩個(gè)工具可以非常方便的對(duì)請(qǐng)求和響應(yīng)進(jìn)行預(yù)處理,需要的朋友可以參考下2022-06-06
SpringBoot+JSON+AJAX+ECharts+Fiddler實(shí)現(xiàn)前后端分離開(kāi)發(fā)可視化
這篇文章主要介紹了SpringBoot+JSON+AJAX+ECharts+Fiddler實(shí)現(xiàn)前后端分離開(kāi)發(fā)可視化,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06
Java UUID 五個(gè)版本的區(qū)別及使用場(chǎng)景小結(jié)
在Java中,UUID是一個(gè)128位的唯一標(biāo)識(shí)符,廣泛應(yīng)用于生成唯一標(biāo)識(shí)符、分布式系統(tǒng)唯一鍵等場(chǎng)景,Java提供的java.util.UUID類支持五種UUID版本,每種具有不同的生成方式和使用場(chǎng)景,本文就來(lái)介紹一下如何使用,感興趣的可以了解一下2024-11-11
Java中Velocity快速對(duì)變量中的引號(hào)特殊字符進(jìn)行轉(zhuǎn)義
Velocity是一個(gè)基于Java的模板引擎,與Freemarker類似,這篇文章主要介紹了Java中Velocity如何對(duì)變量中的引號(hào)特殊字符進(jìn)行轉(zhuǎn)義,主要記錄一下在使用中碰到的要對(duì)引號(hào)特殊字符進(jìn)行轉(zhuǎn)義的問(wèn)題,需要的朋友可以參考下2023-07-07
詳解spring security之httpSecurity使用示例
這篇文章主要介紹了詳解spring security之httpSecurity使用示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-08-08
SpringBoot如何使用Undertow做服務(wù)器
這篇文章主要介紹了SpringBoot如何使用Undertow做服務(wù)器,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07
Spring動(dòng)態(tài)配置計(jì)時(shí)器觸發(fā)時(shí)間的實(shí)例代碼
這篇文章主要介紹了Spring動(dòng)態(tài)配置計(jì)時(shí)器觸發(fā)時(shí)間的實(shí)例代碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-06-06
springboot 注冊(cè)服務(wù)注冊(cè)中心(zk)的兩種方式詳解
本文通過(guò)一個(gè)demo講述一下這兩種注冊(cè)方式,使用的是傳統(tǒng)的向zk注冊(cè)的方案。對(duì)springboot 注冊(cè)zk的相關(guān)知識(shí)感興趣的朋友一起看看吧2018-01-01
Springboot項(xiàng)目Maven依賴沖突的問(wèn)題解決
使用Spring Boot和Maven進(jìn)行項(xiàng)目開(kāi)發(fā)時(shí),依賴沖突是一個(gè)常見(jiàn)的問(wèn)題,本文就來(lái)介紹一下Springboot項(xiàng)目Maven依賴沖突的問(wèn)題解決,具有一定的參考價(jià)值,感興趣的可以了解一下2024-07-07

