<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ## 后臺異常處理 在開發過程中,不可避免的是需要處理各種異常,異常處理方法隨處可見,所以代碼中就會出現大量的`try {...} catch {...} finally {...}` 代碼塊,不僅會造成大量的冗余代碼,而且還影響代碼的可讀性,所以對異常統一處理非常有必要。為此,我們定義了一個統一的異常類`YamiShopBindException` 與異常管理類 `DefaultExceptionHandlerConfig`。 我們先來看下 `YamiShopBindException`的代碼 ```java public class YamiShopBindException extends RuntimeException { private static final long serialVersionUID = -4137688758944857209L; /** * http狀態碼 */ private Integer httpStatusCode; /** * @param httpStatus http狀態碼 */ public YamiShopBindException(YamiHttpStatus httpStatus) { super(httpStatus.getMsg()); this.httpStatusCode = httpStatus.value(); } /** * @param httpStatus http狀態碼 */ public YamiShopBindException(YamiHttpStatus httpStatus, String msg) { super(msg); this.httpStatusCode = httpStatus.value(); } public YamiShopBindException(String msg) { super(msg); this.httpStatusCode = HttpStatus.BAD_REQUEST.value(); } public Integer getHttpStatusCode() { return httpStatusCode; } } ``` `YamiHttpStatus`為我們自定義的返回狀態碼的枚舉類,定義為一個枚舉類,更直觀處理異常返回的狀態碼及異常內容,以后每增加一種異常情況,只需增加一個枚舉實例即可,不用每一種異常都定義一個異常類。 ```java public enum YamiHttpStatus { /** * 客戶端看到401狀態碼時,應該重新登陸 */ UNAUTHORIZED(401, "未授權"), COUPONCANNOTUSETOGETHER(601, "優惠券不能共用"), ; private final int value; private final String msg; YamiHttpStatus(int value, String msg) { this.value = value; this.msg = msg; } public int value() { return this.value; } public String getMsg() { return msg; } public static YamiHttpStatus resolve(int statusCode) { for (YamiHttpStatus status : values()) { if (status.value == statusCode) { return status; } } return null; } } ``` 再來看看 `DefaultExceptionHandlerConfig`類 ```java @Controller @RestControllerAdvice public class DefaultExceptionHandlerConfig { @ExceptionHandler(YamiShopBindException.class) public ResponseEntity<String> unauthorizedExceptionHandler(YamiShopBindException e){ e.printStackTrace(); return ResponseEntity.status(e.getHttpStatusCode()).body(e.getMessage()); } } ``` ## 前臺異常處理 前端請求與相應做了封裝,請求響應的內容會被攔截器所攔截,當后臺返回給前臺特定的狀態碼,前臺將顯示不同報錯信息。請求響應非常常見,我們查看在`src\utils\httpRequest.js`里面的其中一段代碼 ```javascript http.interceptors.response.use(response => { return response }, error => { switch (error.response.status) { case 400: Message.error(error.response.data) break case 401: clearLoginInfo() router.push({ name: 'login' }) break case 405: Message.error('http請求方式有誤') break case 500: Message.error('服務器出了點小差,請稍后再試') break case 501: Message.error('服務器不支持當前請求所需要的某個功能') break } return Promise.reject(error) }) ``` 這里將會統一攔截返回的狀態碼如`400`,進行錯誤提示。 ## RESTful 風格 我們的上述代碼使用http狀態碼對請求進行統一響應,其中最大的 RESTful架構,就是目前最流行的一種互聯網軟件架構。它結構清晰、符合標準、易于理解、擴展方便,所以正得到越來越多網站的采用。 [RESTful概述](https://blog.igevin.info/posts/restful-architecture-in-general/)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看