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

spring boot 全局異常處理方法匯總

 更新時(shí)間:2019年10月16日 14:48:50   作者:全力以赴001  
這篇文章主要介紹了spring boot 全局異常處理方法匯總,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

這篇文章主要介紹了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í)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • springboot中request和response的加解密實(shí)現(xià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ā)可視化

    這篇文章主要介紹了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深入分析動(dòng)態(tài)代理

    Java深入分析動(dòng)態(tài)代理

    動(dòng)態(tài)代理指的是,代理類和目標(biāo)類的關(guān)系在程序運(yùn)行的時(shí)候確定的,客戶通過(guò)代理類來(lái)調(diào)用目標(biāo)對(duì)象的方法,是在程序運(yùn)行時(shí)根據(jù)需要?jiǎng)討B(tài)的創(chuàng)建目標(biāo)類的代理對(duì)象。本文將通過(guò)案例詳細(xì)講解一下Java動(dòng)態(tài)代理的原理及實(shí)現(xiàn),需要的可以參考一下
    2022-07-07
  • Java UUID 五個(gè)版本的區(qū)別及使用場(chǎng)景小結(jié)

    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)義

    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使用示例

    這篇文章主要介紹了詳解spring security之httpSecurity使用示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-08-08
  • SpringBoot如何使用Undertow做服務(wù)器

    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í)例代碼

    這篇文章主要介紹了Spring動(dòng)態(tài)配置計(jì)時(shí)器觸發(fā)時(shí)間的實(shí)例代碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2018-06-06
  • springboot 注冊(cè)服務(wù)注冊(cè)中心(zk)的兩種方式詳解

    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)題解決

    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

最新評(píng)論

万盛区| 马关县| 金川县| 西平县| 柳河县| 九龙坡区| 湖南省| 馆陶县| 屏南县| 安岳县| 彩票| 嘉兴市| 庆云县| 石屏县| 伽师县| 蒲城县| 佛教| 北碚区| 泰和县| 昌邑市| 西乌珠穆沁旗| 黔南| 甘德县| 德格县| 体育| 萍乡市| 岳池县| 额尔古纳市| 分宜县| 金塔县| 嘉定区| 竹溪县| 东丰县| 即墨市| 汕头市| 宁南县| 台南县| 石楼县| 台东市| 彰化市| 元氏县|