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)文章
Java中常見(jiàn)延時(shí)隊(duì)列的實(shí)現(xiàn)方案小結(jié)(建議收藏)
延時(shí)隊(duì)列它要具有隊(duì)列的特性,再給它附加一個(gè)延遲消費(fèi)隊(duì)列消息的功能,也就是說(shuō)可以指定隊(duì)列中的消息在哪個(gè)時(shí)間點(diǎn)被消費(fèi),這篇文章主要介紹了Java中常見(jiàn)延時(shí)隊(duì)列的實(shí)現(xiàn)方案總結(jié),需要的朋友可以參考下2024-04-04SpringBoot整合redis實(shí)現(xiàn)輸入密碼錯(cuò)誤限制登錄功能
遇到這樣的需求需要實(shí)現(xiàn)一個(gè)登錄功能,并且2分鐘之內(nèi)只能輸入5次錯(cuò)誤密碼,若輸入五次之后還沒(méi)有輸入正確密碼,系統(tǒng)將會(huì)將該賬號(hào)鎖定1小時(shí),這篇文章主要介紹了SpringBoot整合redis并實(shí)現(xiàn)輸入密碼錯(cuò)誤限制登錄功能,需要的朋友可以參考下2024-02-02Java+OpenCV調(diào)用攝像頭實(shí)現(xiàn)拍照功能
隨著我們對(duì)環(huán)境、Mat基本使用越來(lái)越熟練、Java Swing也逐步熟悉了起來(lái)。本文將通過(guò)OpenCV驅(qū)動(dòng)攝像頭實(shí)現(xiàn)識(shí)臉和拍照功能,需要的可以參考一下2022-03-03Apache Commons fileUpload文件上傳多個(gè)示例分享
這篇文章主要為大家分享了Apache Commons fileUpload文件上傳4個(gè)示例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10一文教會(huì)你如何從0到1搭建一個(gè)SpringBoot項(xiàng)目
今天剛好學(xué)習(xí)到SpringBoot,就順便記錄一下吧,下面這篇文章主要給大家介紹了關(guān)于如何從0到1搭建一個(gè)SpringBoot項(xiàng)目的相關(guān)資料,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下2024-01-01Quarkus云原生開(kāi)篇java框架簡(jiǎn)介
Quarkus?是小紅帽開(kāi)源的專門(mén)針對(duì)云容器環(huán)境優(yōu)化的云原生java框架,博主接下來(lái)的項(xiàng)目估計(jì)都會(huì)使用這個(gè)框架來(lái)開(kāi)發(fā),相關(guān)的問(wèn)題都會(huì)記錄在這個(gè)系列,本文是個(gè)開(kāi)篇2022-02-02SpringBoot異步實(shí)現(xiàn) 的8種方式
在同步操作中,執(zhí)行到?發(fā)送短信?的時(shí)候,我們必須等待這個(gè)方法徹底執(zhí)行完才能執(zhí)行?贈(zèng)送積分?這個(gè)操作,如果?贈(zèng)送積分?這個(gè)動(dòng)作執(zhí)行時(shí)間較長(zhǎng),發(fā)送短信需要等待,這就是典型的同步場(chǎng)景,這篇文章主要介紹了SpringBoot異步實(shí)現(xiàn) 的8種方式,需要的朋友可以參考下2023-11-11