在咱們作後端應用的時候,先後端分離的狀況下,咱們常常會定義一個數據格式,一般會包含code
,message
,data
這三個必不可少的信息來方便咱們的交流,下面咱們直接來看代碼java
package indi.viyoung.viboot.util; import java.util.Properties; /** * 統必定義返回類 * * @author yangwei * @since 2018/12/20 */ public class ReturnVO { private static final Properties properties = ReadPropertiesUtil.getProperties(System.getProperty("user.dir") + "/viboot-common/src/main/resources/response.properties"); /** * 返回代碼 */ private String code; /** * 返回信息 */ private String message; /** * 返回數據 */ private Object data; public Object getData() { return data; } public void setData(Object data) { this.data = data; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } /** * 默認構造,返回操做正確的返回代碼和信息 */ public ReturnVO() { this.setCode(properties.getProperty(ReturnCode.SUCCESS.val())); this.setMessage(properties.getProperty(ReturnCode.SUCCESS.msg())); } /** * 構造一個返回特定代碼的ReturnVO對象 * @param code */ public ReturnVO(ReturnCode code) { this.setCode(properties.getProperty(code.val())); this.setMessage(properties.getProperty(code.msg())); } /** * 默認值返回,默認返回正確的code和message * @param data */ public ReturnVO(Object data) { this.setCode(properties.getProperty(ReturnCode.SUCCESS.val())); this.setMessage(properties.getProperty(ReturnCode.SUCCESS.msg())); this.setData(data); } /** * 構造返回代碼,以及自定義的錯誤信息 * @param code * @param message */ public ReturnVO(ReturnCode code, String message) { this.setCode(properties.getProperty(code.val())); this.setMessage(message); } /** * 構造自定義的code,message,以及data * @param code * @param message * @param data */ public ReturnVO(ReturnCode code, String message, Object data) { this.setCode(code.val()); this.setMessage(message); this.setData(data); } @Override public String toString() { return "ReturnVO{" + "code='" + code + '\'' + ", message='" + message + '\'' + ", data=" + data + '}'; } }
在這裏,我提供了幾個構造方法以供不一樣狀況下使用。代碼的註釋已經寫得很清楚了,你們也能夠應該看的比較清楚~git
細心的同窗可能發現了,我單獨定義了一個ReturnCode
枚舉類用於存儲代碼和返回的Message:github
package indi.viyoung.viboot.util; /** * @author yangwei * @since 2018/12/20 */ public enum ReturnCode { /** 操做成功 */ SUCCESS("SUCCESS_CODE", "SUCCESS_MSG"), /** 操做失敗 */ FAIL("FAIL_CODE", "FAIL_MSG"), /** 空指針異常 */ NullpointerException("NPE_CODE", "NPE_MSG"), /** 自定義異常之返回值爲空 */ NullResponseException("NRE_CODE", "NRE_MSG"); private ReturnCode(String value, String msg){ this.val = value; this.msg = msg; } public String val() { return val; } public String msg() { return msg; } private String val; private String msg; }
這裏,我並無將須要存儲的數據直接放到枚舉中,而是放到了一個配置文件中,這樣既能夠方便咱們進行相關信息的修改,而且閱讀起來也是比較方便。後端
SUCCESS_CODE=2000 SUCCESS_MSG=操做成功 FAIL_CODE=5000 FAIL_MSG=操做失敗 NPE_CODE=5001 NPE_MSG=空指針異常 NRE_CODE=5002 NRE_MSG=返回值爲空
注意,這裏的屬性名和屬性值分別與枚舉類中的value和msg相對應,這樣,咱們才能夠方便的去經過I/O流去讀取。app
這裏須要注意一點,若是你使用的是IDEA編輯器,須要修改如下的配置,這樣你編輯配置文件的時候寫的是中文,實際上保存的是ASCII字節碼。前後端分離
下面,來看一下讀取的工具類:編輯器
package indi.viyoung.viboot.util; import java.io.*; import java.util.Iterator; import java.util.Properties; /** * 讀取*.properties中的屬性 * @author vi * @since 2018/12/24 7:33 PM */ public class ReadPropertiesUtil { public static Properties getProperties(String propertiesPath){ Properties properties = new Properties(); try { InputStream inputStream = new BufferedInputStream(new FileInputStream(propertiesPath)); properties.load(inputStream); } catch (IOException e) { e.printStackTrace(); } return properties; } }
這裏我直接寫了一個靜態的方法,傳入的參數是properties文件的位置,這樣的話,本文最初代碼中的也就獲得瞭解釋。ide
private static final Properties properties = ReadPropertiesUtil.getProperties(System.getProperty("user.dir") + "/viboot-common/src/main/resources/response.properties");
@RequestMapping("/test") public ReturnVO test(){ try { //省略 //省略 } catch (Exception e) { e.printStackTrace(); } return new ReturnVO(); }
下面咱們能夠去訪問這個接口,看看會獲得什麼:工具
可是,如今問題又來了,由於try...catch...
的存在,老是會讓代碼變得重複度很高,一個接口你都至少要去花三到十秒去寫這個接口,若是不知道編輯器的快捷鍵,更是一種噩夢。咱們只想全心全意的去關注實現業務,而不是花費大量的時間在編寫一些重複的"剛需"代碼上。測試
(這裏,我只是對全局異常處理進行一個簡單的講解,後面也就是下一節中會詳細的講述)
/** * 統一封裝返回值和異常處理 * * @author vi * @since 2018/12/20 6:09 AM */ @Slf4j @Aspect @Order(5) @Component public class ResponseAop { private static final Properties properties = ReadPropertiesUtil.getProperties(System.getProperty("user.dir") + "/viboot-common/src/main/resources/response.properties"); /** * 切點 */ @Pointcut("execution(public * indi.viyoung.viboot.*.controller..*(..))") public void httpResponse() { } /** * 環切 */ @Around("httpResponse()") public ReturnVO handlerController(ProceedingJoinPoint proceedingJoinPoint) { ReturnVO returnVO = new ReturnVO(); try { //獲取方法的執行結果 Object proceed = proceedingJoinPoint.proceed(); //若是方法的執行結果是ReturnVO,則將該對象直接返回 if (proceed instanceof ReturnVO) { returnVO = (ReturnVO) proceed; } else { //不然,就要封裝到ReturnVO的data中 returnVO.setData(proceed); } } catch (Throwable throwable) { //若是出現了異常,調用異常處理方法將錯誤信息封裝到ReturnVO中並返回 returnVO = handlerException(throwable); } return returnVO; } /** * 異常處理 */ private ReturnVO handlerException(Throwable throwable) { ReturnVO returnVO = new ReturnVO(); //這裏須要注意,返回枚舉類中的枚舉在寫的時候應該和異常的名稱相對應,以便動態的獲取異常代碼和異常信息 //獲取異常名稱的方法 String errorName = throwable.toString(); errorName = errorName.substring(errorName.lastIndexOf(".") + 1); //直接獲取properties文件中的內容 returnVO.setMessage(properties.getProperty(ReturnCode.valueOf(errorName).msg())); returnVO.setCode(properties.getProperty(ReturnCode.valueOf(errorName).val())); return returnVO; } }
若是,咱們須要在每個項目中均可以這麼去作,須要將這個類放到一個公用的模塊中,而後在pom中導入這個模塊
<dependency> <groupId>indi.viyoung.course</groupId> <artifactId>viboot-common</artifactId> <version>1.0-SNAPSHOT</version> </dependency>
這裏須要注意一點,必須保證你的切點的正確書寫!!不然就會致使切點無效,同時須要在啓動類中配置:
@ComponentScan(value = "indi.viyoung.viboot.*")
導入的正是common
包下的全部文件,以保證能夠將ResponseAop
這個類加載到Spring的容器中。
下面咱們來測試一下,訪問咱們通過修改後的編寫的findAll
接口:
@RequestMapping("/findAll") public Object findAll(){ return userService.list(); }
PS:這裏我將返回值統一爲Object,以便數據存入data
,實際類型應是Service
接口的返回類型。若是沒有返回值的話,那就能夠new
一個ReturnVO
對象直接經過構造方法賦值便可。關於返回類型爲ReturnVO
的判斷,代碼中也已經作了特殊的處理,並不是存入data
,而是直接返回。
下面,咱們修改一下test方法,讓他拋出一個咱們自定義的查詢返回值爲空的異常:
@RequestMapping("/test") public ReturnVO test(){ throw new NullResponseException(); }
下面,咱們再來訪問如下test接口:
能夠看到,正如咱們properties中定義的那樣,咱們獲得了咱們想要的消息。
源碼能夠去github或者碼雲上進行下載,後續的例子都會同步更新。
原創文章,文筆有限,才疏學淺,文中如有不正之處,萬望告知。