1. 程式人生 > >Java的新專案學成線上筆記-day3(十二)

Java的新專案學成線上筆記-day3(十二)

5.4 不可預知異常處理
5.4.1 定義異常捕獲方法 5.4.1.1 異常丟擲測試
使用postman測試新增頁面,不輸入cmsPost資訊,提交,報錯資訊如下:

org.springframework.http.converter.HttpMessageNotReadableException
此異常是springMVC在進行引數轉換時報的錯誤。

具體的響應的資訊為:

{ 
   "timestamp": 1528712906727, 
   "status": 400,  
  "error": "Bad Request",   
 "exception": "org.springframework.http.converter.HttpMessageNotReadableException",  
  "message": "Required request body is missing: public  com.xuecheng.framework.domain.cms.response.CmsPageResult  com.xuecheng.manage_cms.web.controller.CmsPageController.add(com.xuecheng.framework.domain.cms.C msPage)",   
 "path": "/cms/page/add"
}

上邊的響應資訊在客戶端是無法解析的。
在異常捕獲類中新增對Exception異常的捕獲:

@ExceptionHandler(Exception.class) @ResponseBody public ResponseResult exception(Exception exception){  
  //記錄日誌  
   LOGGER.error("catch exception:{}",exception.getMessage());    
   return null; 
}

5.4.1.2 異常捕獲方法
針對上邊的問題其解決方案是:
1、我們在map中配置HttpMessageNotReadableException和錯誤程式碼。 2、在異常捕獲類中對Exception異常進行捕獲,並從map中獲取異常型別對應的錯誤程式碼,如果存在錯誤程式碼則返 回此錯誤,否則統一返回99999錯誤。
具體的開發實現如下:
1、在通用錯誤程式碼類CommCode中配置非法引數異常


INVALID_PARAM(false,10003,"非法引數!"),

2、在異常捕獲類中配置 HttpMessageNotReadableException 為非法引數異常。
異常捕獲類程式碼如下:

package com.xuecheng.framework.exception;  
 import com.google.common.collect.ImmutableMap;
import com.xuecheng.framework.model.response.CommonCode; 
import com.xuecheng.framework.model.response.ResponseResult; 
import com.xuecheng.framework.model.response.ResultCode; 
import org.slf4j.Logger; 
import org.slf4j.LoggerFactory; 
import org.springframework.http.converter.HttpMessageNotReadableException;
 import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseBody;  
   /**  * @author Administrator  * @version 1.0  * @create 2018‐06‐11 17:16  **/ @ControllerAdvice public class ExceptionCatch {     private static final Logger LOGGER = LoggerFactory.getLogger(ExceptionCatch.class);   
    //使用EXCEPTIONS存放異常型別和錯誤程式碼的對映,ImmutableMap的特點的一旦建立不可改變,並且執行緒安全     private static ImmutableMap<Class<? extends Throwable>,ResultCode> EXCEPTIONS;   
  //使用builder來構建一個異常型別和錯誤程式碼的異常 
    protected static ImmutableMap.Builder<Class<? extends Throwable>,ResultCode> builder =  ImmutableMap.builder();    
   //捕獲Exception異常  
   @ResponseBody   
  @ExceptionHandler(Exception.class)  
  public ResponseResult exception(Exception e) {   
     LOGGER.error("catch exception : {}\r\nexception: ",e.getMessage(), e);    
     if(EXCEPTIONS == null)      
      EXCEPTIONS = builder.build();   
      final ResultCode resultCode = EXCEPTIONS.get(e.getClass());     
    final ResponseResult responseResult;     
    if (resultCode != null) {     
       responseResult = new ResponseResult(resultCode);    
     } else {     
       responseResult = new ResponseResult(CommonCode.SERVER_ERROR);    
     }     
   return responseResult;    
 }    
  //捕獲 CustomException異常 
    @ExceptionHandler(CustomException.class)  
  @ResponseBody   
  public ResponseResult customException(CustomException e) { 
       LOGGER.error("catch exception : {}\r\nexception: ",e.getMessage(), e);    
     ResultCode resultCode = e.getResultCode();    
     ResponseResult responseResult = new ResponseResult(resultCode);     
      return responseResult;
    }

static{  
      //在這裡加入一些基礎的異常型別判斷    
     builder.put(HttpMessageNotReadableException.class,CommonCode.INVALIDPARAM); 
    }
}

5.4.3 異常處理測試
仍然模擬“問題測試”中的測試步驟,異常結果為“非法引數”。
6 實戰
此部分為自學內容,根據今天所學知識完成下邊的任務。 6.1 查詢條件完善
頁面查詢條件增加:頁面名稱、頁面型別。
頁面名稱對應CmsPage模型類中的pageName屬性。 頁面型別對應CmsPage模型類中的pageType屬性。
查詢要求:
頁面名稱:模糊查詢
頁面型別:精確匹配,頁面型別包括:靜態和動態,在資料庫中靜態用“0”表示,動態用“1”表示。 6.2 頁面屬性增加DataUrl
在CmsPage.java模型型別中有一個dataUrl屬性,此屬性在頁面靜態化時需要填寫。
本需求要求:
1、在新增頁面增加dataUrl輸入框,並支援新增。
2、在修改頁面增加dataUrl輸入框,並支援修改。