1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

        求求你不要滿屏的try...catch了,這樣寫真香!

        共 3860字,需瀏覽 8分鐘

         ·

        2021-02-21 13:03


        超全面!Java核心知識總結

        超全面!Java核心知識總結

        超全面!Java核心知識總結



        作者:小李子說程序

        www.toutiao.com/i6878184496945070604

        前言

        軟件開發(fā)springboot項目過程中,不可避免的需要處理各種異常,spring mvc 架構中各層會出現大量的try {...} catch {...} finally {...}代碼塊,不僅有大量的冗余代碼,而且還影響代碼的可讀性。這樣就需要定義個全局統(tǒng)一異常處理器,以便業(yè)務層再也不必處理異常。

        推薦理由

        • 代碼復制到項目中通過簡單的配置即可實現

        • 可以靈活的根據自己的業(yè)務異常進行更細粒度的擴展

        實踐

        1 封裝統(tǒng)一返回結果類

        源代碼

        public?class?AjaxResult?{
        ?//是否成功
        ????private?Boolean?success;
        ????//狀態(tài)碼
        ????private?Integer?code;
        ????//提示信息
        ????private?String?msg;
        ????//數據
        ????private?Object?data;
        ????public?AjaxResult()?{

        ????}
        ????//自定義返回結果的構造方法
        ????public?AjaxResult(Boolean?success,Integer?code,?String?msg,Object?data)?{
        ????????this.success?=?success;
        ????????this.code?=?code;
        ????????this.msg?=?msg;
        ????????this.data?=?data;
        ????}
        ????//自定義異常返回的結果
        ????public?static?AjaxResult?defineError(BusinessException?de){
        ?????AjaxResult?result?=?new?AjaxResult();
        ????????result.setSuccess(false);
        ????????result.setCode(de.getErrorCode());
        ????????result.setMsg(de.getErrorMsg());
        ????????result.setData(null);
        ????????return?result;
        ????}
        ????//其他異常處理方法返回的結果
        ????public?static?AjaxResult?otherError(ErrorEnum?errorEnum){
        ?????AjaxResult?result?=?new?AjaxResult();
        ????????result.setMsg(errorEnum.getErrorMsg());
        ????????result.setCode(errorEnum.getErrorCode());
        ????????result.setSuccess(false);
        ????????result.setData(null);
        ????????return?result;
        ????}
        ?public?Boolean?getSuccess()?{
        ??return?success;
        ?}
        ?public?void?setSuccess(Boolean?success)?{
        ??this.success?=?success;
        ?}
        ?public?Integer?getCode()?{
        ??return?code;
        ?}
        ?public?void?setCode(Integer?code)?{
        ??this.code?=?code;
        ?}
        ?public?String?getMsg()?{
        ??return?msg;
        ?}
        ?public?void?setMsg(String?msg)?{
        ??this.msg?=?msg;
        ?}
        ?public?Object?getData()?{
        ??return?data;
        ?}
        ?public?void?setData(Object?data)?{
        ??this.data?=?data;
        ?}
        ????
        }

        2 自定義異常封裝類

        源碼:

        public?class?BusinessException?extends?RuntimeException?{
        ?private?static?final?long?serialVersionUID?=?1L;
        ?/**
        ??*?錯誤狀態(tài)碼
        ??*/

        ?protected?Integer?errorCode;
        ?/**
        ??*?錯誤提示
        ??*/

        ?protected?String?errorMsg;

        ?public?BusinessException(){

        ?????}

        ?public?BusinessException(Integer?errorCode,?String?errorMsg)?{
        ?????????this.errorCode?=?errorCode;
        ?????????this.errorMsg?=?errorMsg;
        ?????}

        ?public?Integer?getErrorCode()?{
        ??return?errorCode;
        ?}

        ?public?void?setErrorCode(Integer?errorCode)?{
        ??this.errorCode?=?errorCode;
        ?}

        ?public?String?getErrorMsg()?{
        ??return?errorMsg;
        ?}

        ?public?void?setErrorMsg(String?errorMsg)?{
        ??this.errorMsg?=?errorMsg;
        ?}
        }

        3 錯誤枚舉,拒絕硬編碼

        源碼

        public?enum?ErrorEnum?{
        ?//?數據操作錯誤定義
        ?SUCCESS(200,?"成功"),
        ?NO_PERMISSION(403,"你沒得權限"),
        ?NO_AUTH(401,"未登錄"),
        ?NOT_FOUND(404,?"未找到該資源!"),
        ?INTERNAL_SERVER_ERROR(500,?"服務器異常請聯系管理員"),
        ?;

        ?/**?錯誤碼?*/
        ?private?Integer?errorCode;

        ?/**?錯誤信息?*/
        ?private?String?errorMsg;

        ?ErrorEnum(Integer?errorCode,?String?errorMsg)?{
        ??this.errorCode?=?errorCode;
        ??this.errorMsg?=?errorMsg;
        ?}

        ????public?Integer?getErrorCode()?{
        ????????return?errorCode;
        ????}

        ????public?String?getErrorMsg()?{
        ????????return?errorMsg;
        ????}
        }

        4 全局異常處理類

        源碼

        /**
        ?*?全局異常處理器
        ?*?
        ?*/

        @RestControllerAdvice
        public?class?GlobalExceptionHandler
        {
        ????private?static?final?Logger?log?=?LoggerFactory.getLogger(GlobalExceptionHandler.class);



        ????/**
        ?????*?處理自定義異常
        ?????*
        ?????*/

        ????@ExceptionHandler(value?=?BusinessException.class)
        ????public?AjaxResult?bizExceptionHandler(BusinessException?e)?
        {
        ?????log.error(e.getMessage(),?e);
        ????????return?AjaxResult.defineError(e);
        ????}

        ????/**
        ?????*處理其他異常
        ?????*
        ?????*/

        ????@ExceptionHandler(value?=?Exception.class)
        ????public?AjaxResult?exceptionHandler(?Exception?e)?
        {
        ??????log.error(e.getMessage(),?e);
        ????????return?AjaxResult.otherError(ErrorEnum.INTERNAL_SERVER_ERROR);
        ???????
        ????}

        }

        5 測試

        返回結果:


        如有文章對你有幫助,

        在看”和轉發(fā)是對我最大的支持!


        最后,分享一份?Java核心知識手冊?

        包含了JVM、Java集合、Java多線程并發(fā)、Java基礎、Spring原理、微服務、Netty和RPC、網絡、日志、Zookeeper、KafKa、RabbitMQ、Hbase、MongoDB、Cassandra、設計模式、負載均衡、數據庫、一致性算法、Java算法、數據結構、加密算法、分布式緩存、Hadoop、Spark、Storm...

        點擊下方圖片,即可獲取



        瀏覽 51
        點贊
        評論
        收藏
        分享

        手機掃一掃分享

        分享
        舉報
        評論
        圖片
        表情
        推薦
        點贊
        評論
        收藏
        分享

        手機掃一掃分享

        分享
        舉報
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            6080yy电影在线看 | 操逼免费看 | 色国产精品女五丁香五月五月 | 久草欧美 | 天天干干 | 天天摸夜夜爽 | 插插看| 日穴网站 | 国产寡妇婬乱A毛片视频3 | 国产探花视频在线 |