まず、キャプチャ例外処理例外なくコード、マニュアル撮影に:
@GetMapping( "/ ERROR1" ) パブリック文字列ERROR1(){ int型私は10/0 = 。 リターン「TEST1」。 } @ResponseBody @GetMapping( "/ ERROR2" ) パブリックマップ<文字列、文字列> ERROR2(){ 地図 <文字列、文字列>結果= 新しい HashMapの<>(16 )。 試す{ int型私は10/0 = 。 result.put( "コード"、 "200" )。 result.put( "データ"、 "具体返回的结果集" )。(例外e){ result.put( "コード"、 "500" )。 result.put( "メッセージ"、 "请求错误" )。 } 戻り値の結果; }
問題の一つは、様々な問題のために、これ以上言って、そうでない異常な統一されたキャプチャの必要性
1、輸入に依存
<依存> <groupIdを> org.springframework.boot </ groupIdを> <たartifactId>春・ブート・スターター・ウェブ</たartifactId> </依存関係>
2、カスタム例外クラス
パッケージcom.example.demo.entity。
輸入lombok.Data。
@data
パブリッククラスはErrorResponse {
プライベートint型のコード。
プライベート文字列メッセージ。
公共はErrorResponse(INTコード、文字列メッセージ){
this.code =コード。
this.message =メッセージ。
}
パブリックはErrorResponse(){
}
公共はErrorResponse OK(文字メッセージ){
this.code = 200。
this.message =メッセージ。
これを返します。
}
公共ボイドOK(){
this.code = 200。
this.message = "";
}
}
3、異常なテンプレートの定義
パッケージcom.example.demo.entity。 輸入lombok.Data。 @data パブリック クラスはErrorResponse { プライベート int型のコード。 プライベート文字列メッセージ。 公共はErrorResponse(int型コード、文字列メッセージ){ この .CODE = コード。 この .message = メッセージ。 } }
4、異常インターセプター
このステップの焦点は、特別な注意を必要とする場合、
@ControllerAdviceキャプチャコントローラ層スローされた例外あなたが@ResponseBody情報を追加する場合、JSON形式で返されました。
@RestControllerAdviceは@ControllerAdvice @ResponseBodyの組み合わせに対応します。
例外クラスの@ExceptionHandler単一の一種、コードの減少の繰り返し率は、複雑さを軽減します。GlobalExceptionHandler例外通知クラスを定義し、@ExceptionHandlerに珍しいキャッチを達成するために定義されたメソッドを追加することができますクラスを作成し、@RestControllerAdviceに注釈を追加...
パッケージcom.example.demo.utils。 輸入com.example.demo.entity.ErrorResponse。 輸入org.springframework.http.HttpHeaders。 輸入org.springframework.http.HttpStatus。 輸入org.springframework.http.ResponseEntity。 輸入org.springframework.lang.Nullable; 輸入org.springframework.web.bind.MethodArgumentNotValidException。 輸入org.springframework.web.bind.annotation.ExceptionHandler。 輸入org.springframework.web.bind.annotation.RestControllerAdvice。 輸入org.springframework.web.context.request.WebRequest; インポートorg.springframework.web.method.annotation.MethodArgumentTypeMismatchException; 輸入org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler。 インポートのjavax.servlet.http.HttpServletRequest; インポートのjavax.servlet.http.HttpServletResponse; @RestControllerAdvice パブリック クラス GlobalExceptionHandlerは延びResponseEntityExceptionHandler { @ExceptionHandler(LclException。クラス) パブリックはErrorResponse lclExceptionHandler(HttpServletRequestの要求、最終的な例外E、HttpServletResponseの応答){ response.setStatus(HttpStatus.BAD_REQUEST.value())。 LclException例外 =(LclException)E; 返す 新しいはErrorResponse(exception.getCodeを()、exception.getMessage()); } @ExceptionHandler(のRuntimeException クラス) パブリックはErrorResponse runtimeExcetionHandler(HttpServletRequestの要求、最終的な例外E、HttpServletResponseの応答){ response.setStatus(HttpStatus.BAD_REQUEST.value())。 RuntimeException例外 = (のRuntimeException)E。 返す 新しいはErrorResponseを(400 、exception.getMessage()); } @Override 保護 ResponseEntity <OBJECT> handleExceptionInternal(例外EX、@Nullableオブジェクト本体、HttpHeadersヘッダ、ステータスはhttpStatus、WebRequestクラスリクエスト){ 場合(元のinstanceof MethodArgumentNotValidException){ MethodArgumentNotValidException例外 = (MethodArgumentNotValidException)EX。 返す 新しい ResponseEntity <>(新はErrorResponse(status.value()、exception.getBindingResult()getAllErrors()を得る(0。。).getDefaultMessage())、状態)。 } であれば(EX のinstanceof MethodArgumentTypeMismatchException){ MethodArgumentTypeMismatchException例外 =(MethodArgumentTypeMismatchException)EX; を返す 新しい新しい ResponseEntity <>(新新はErrorResponse(status.value()、 "引数の変換例外を" )、ステータス); } を返す 新しい新しい ResponseEntity <>(新新はErrorResponse(status.value()、「引数の変換例外を" )、ステータス); } }
5、テストクラス
@ResponseBody
@GetMapping( "/ error3")
公衆はErrorResponseのerror3(整数NUM)LclException {スロー
(NUM == NULL)場合、{
( "NUM不允许为空"、12345)新しいLclExceptionをスローします。
}
iが10 / numは= int型。
リターン(新はErrorResponse())OK(i "は" +)。
}
6、テスト