グローバル例外ハンドラSpringBoot

参考:ここに

UI統一フォーマットタイプのリターン

1  パブリック クラスCommonReturnTypeは{
 2      // UIにデータを返す
3。     プライベート・オブジェクト・データ;
 4      // UIの状態を返す
5つの。     プライベート文字列状態;
 6  
。7      パブリック 静的{(結果オブジェクト)を作成CommonReturnType
 8つの。         // デフォルトの戻りステータスが成功であります
9          リターン CommonReturnType.create(結果が「成功」);
 10      }
 11      公共 静的CommonReturnType {(文字列ステータスの結果オブジェクト)を作成し
 12である          CommonReturnTypeタイプ= 新しい新CommonReturnType();
13          type.setData(結果)。
14          type.setStatus(ステータス)。
15          戻り値のタイプ。
16      }
 17  
18      公的オブジェクトのgetData(){
 19の         リターンデータ。
20      }
 21の 
22      公共の ボイドのsetData(オブジェクトデータ){
 23          この .DATA = データ。
24      }
 25の 
26      公共の文字列のgetStatus(){
 27の         リターン・ステータス。
28      }
 29  
30      公共 のボイドsetStatus(文字列状態){
 31          この .status = ステータス。
32      }
 33 }

 

カスタム例外フォーマット

1  パブリック インターフェースCommonError {
 2      公共の INT getErrCode()。
3      パブリックストリングgetErrMsg()。
4      公共CommonError setErrMsg(文字列たerrMsg)。
5 }

 

1つの 公共 列挙 EmBusinessError 実装CommonErrorは{
 2      // エラー10,000ジェネリック型を始める
。3      PARAMETER_VALIDATION_ERROR(10001、 "無効なパラメータ" )、
 4  
。5      // 未知のエラー
。6      UNKNOW_ERROR(10002、 "不明なエラー" )、
 7  
。8      // 2万エラーに関連のユーザ情報で始まる
9      (20001、 "ユーザーが存在しない" USER_NOT_EXIST 、)
 10      User_LOGIN_ERROR(20002、 "電話番号やパスワードが間違っている" 、)
 11      User_NOT_LOGIN(20003、 "ないユーザログイン" 、)
 12  
13      // 初め30,000トランザクション情報が異常である
14      」、STOCK_NOT_ENOUGH(30001在庫不足」15  16      プライベート int型ERRCODE。
17      プライベート文字列たerrMsg。
18  
19      EmBusinessError(int型のERRCODE、ストリングたerrMsg){
 20          この .errCode = ERRCODE。
21          この .errMsg = たerrMsg。
22      }
 23  
24      @Override
 25      公衆 INT getErrCode(){
 26          リターン この.errCode。
27      }
 28  
29      @Override
 30      公衆ストリングgetErrMsg(){
 31          リターン この.errMsg。
32      }
 33  
34      @Override
 35      公衆CommonError setErrMsg(文字列たerrMsg){
 36          この .errMsg = たerrMsg。
37          返す これを38      }
 39 }

 

1つの パブリック クラス BusinessException 延び例外実装CommonError {
 2      プライベートCommonError CommonError;
 3  
。4      // EmBussinessErrorは、異常トラフィックのパラメータを渡すように構成された直接受信
5。     公共BusinessException(CommonError CommonError){
 6。         スーパー();
 7          この .commonError = CommonError;
 8      }
 9。 
10      @ 構築カスタムビジネス例外たerrMsg受け入れ
11。     公共BusinessException(CommonError CommonError、ストリングたerrMsg){
 12である         スーパー();
 13れます         この .commonError = commonError。
14          この.commonError.setErrMsg(たerrMsg)。
15      }
 16  
17      @Override
 18      公衆 INT getErrCode(){
 19          リターン この.commonError.getErrCode()。
20      }
 21  
22      @Override
 23      公衆ストリングgetErrMsg(){
 24          リターン この.commonError.getErrMsg()。
25      }
 26  
27      @Override
 28      公衆CommonError setErrMsg(文字列たerrMsg){
 29         戻る この.commonError.setErrMsg(たerrMsg)を、
30      }
 31 }

 

グローバル例外ハンドラ

1つの インポートcom.wu.error.BusinessException。
2  インポートcom.wu.error.EmBusinessError。
3  輸入com.wu.response.CommonReturnType。
4  インポートorg.springframework.web.bind.ServletRequestBindingException。
5  輸入org.springframework.web.bind.annotation.ControllerAdvice。
6  インポートorg.springframework.web.bind.annotation.ExceptionHandler。
7  インポートorg.springframework.web.bind.annotation.ResponseBody。
8  インポートorg.springframework.web.servlet.NoHandlerFoundException。
9  
10  インポートjavax.servlet.http.HttpServletRequest;
11  インポートのjavax.servlet.http.HttpServletResponse。
12  輸入java.util.HashMapを。
13  インポートjava.util.Map。
14  
15  @ControllerAdvice
 16  パブリック クラスGlobalExceptionHandler {
 17      @ExceptionHandler(例外。クラス18      @ResponseBody
 19      公衆CommonReturnType doError(HttpServletRequestのHttpServletRequestを、HttpServletResponseのHttpServletResponseの、例外例){
 20          ex.printStackTrace();
21          地図<文字列、オブジェクト> responseData =新しい HashMapの<> ();
22          であれば(EX のinstanceof BusinessException){
 23              BusinessException businessException = (BusinessException)EX。
24              responseData.put( "ERRCODE" 、businessException.getErrCode())。
25              responseData.put( "たerrMsg" 、businessException.getErrMsg())。
26          } そう であれば(元のinstanceof ServletRequestBindingException){
 27              responseData.put( "ERRCODE" 、EmBusinessError.UNKNOW_ERROR.getErrCode())。
28             responseData.put( "たerrMsg"、 "URL绑定路由问题" );
29          } {
 30              responseData.put( "ERRCODE" 、EmBusinessError.UNKNOW_ERROR.getErrCode())。
31              responseData.put( "たerrMsg" 、EmBusinessError.UNKNOW_ERROR.getErrMsg())。
32          }
 33          リターン CommonReturnType.create(responseData、 "失敗" )。
34      }
 35 }

 

おすすめ

転載: www.cnblogs.com/wuba/p/11420597.html