簡単SpringBootを使用すると、グローバルな例外を取得するには

SpringBoot 単純化するために  Spring 、アプリケーションの作成を実行し、デバッグ、このような製品の誕生などの問題、の範囲に展開私たちはより良いではなく、外部のXML構成よりも、ビジネスそのものに集中できるように、自動組立の特性を、我々は単にの導入に関連した規範に従ってください簡単にWEBプロジェクトをビルドすることができます依存性

実際のプロジェクトの開発は、プログラムは常に分業の場合には、異常を避けることができない、呼び出し書くために、フロントエンドインターフェースを維持し、多くの場合、特に当社のサーバー開発者として、起こる異常の広い範囲であります誤った情報が直接ユーザーに直接さらされると起こる、この経験を想像することができ、異常の詳細情報については、非常に大きな助けを提供する傾向がある***のための...

まず、異常を見て

例外を要求するためのシンプルなインターフェイス

@GetMapping("/test1")
public String test1() {
    // TODO 这里只是模拟异常,假设业务处理的时候出现错误了,或者空指针了等等...
    int i = 10 / 0;
    return "test1";
}

見つけたとき、それをアクセスするためにブラウザを開いて

簡単SpringBootを使用すると、グローバルな例外を取得するには ブラウザでの例外情報

あるいは、ある  postman シミュレーションツール

簡単SpringBootを使用すると、グローバルな例外を取得するには

このインターフェースは、第三者または自社システムへの呼び出しである場合、それは暴走だった...この誤算を参照してください。

愚かな方法(非常に推奨されません)

使用try-catchモードは、手動で私は多くの人が(例えば:Resultオブジェクトとしてカプセル化された)ようなコードを見ていると信じて、異常情報をキャプチャし、対応する結果セットを返します。けれども、エラーさらさ問題、同じ欠点を解決するための間接的な方法過度に対応し、符号量の数を増加させることときに異常ことも明らかであるcatchビジネス例外及びエラーコードので、最良の方法の間の一致が簡単であると管理することがますます困難アップ層グローバル・コントロールを設定....

@GetMapping("/test2")
public Map<String, String> test2() {
    Map<String, String> result = new HashMap<>(16);
    // TODO 直接捕获所有代码块,然后在 cache
    try {
        int i = 10 / 0;
        result.put("code", "200");
        result.put("data", "具体返回的结果集");
    } catch (Exception e) {
        result.put("code", "500");
        result.put("message", "请求错误");
    }
    return result;
}

特定のコード

上記の読書を通してあなたは我々はすべておおよその異常がグローバルにキャプチャする必要がある理由を理解することができ、その後を見て  Spring Boot 提供するソリューション

輸入依存度

では  pom.xml アドオン  spring-boot-starter-web 依存性のことが可能

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

カスタム例外

アプリケーション開発プロセスでは、システム自体の異常で使用される珍しい、さまざまなビジネス・シナリオに加えてするタイトルで、同じではありません  グローバル例外を取得しやすいよう  、自分自身の例外のより適切な定義をキャプチャする方法を参照してください...

package com.battcn.exception;

/**
 * 自定义异常
 *
 * @author Levin
 * @since 2018/6/1 0001
 */
public class CustomException extends RuntimeException {

    private static final long serialVersionUID = 4564124491192825748L;

    private int code;

    public CustomException() {
        super();
    }

    public CustomException(int code, String message) {
        super(message);
        this.setCode(code);
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }
}

異常なテンプレート

リターン・フォーマットを定義し、この異常なスタイルに異常情報をより統一されています

package com.battcn.exception;

/**
 * @author Levin
 * @since 2018/6/1 0001
 */
public class ErrorResponseEntity {

    private int code;
    private String message;
    // 省略 get set 
}

制御層

よく見ると、通常の書き込みコードは区別を欠い通常ではありません、見て、その後、急いではありません....

package com.battcn.controller;

import com.battcn.exception.CustomException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 全局异常演示
 *
 * @author Levin
 * @since 2018/5/31 0031
 */
@RestController
public class ExceptionController {

    @GetMapping("/test3")
    public String test3(Integer num) {
        // TODO 演示需要,实际上参数是否为空通过 @RequestParam(required = true)  就可以控制
        if (num == null) {
            throw new CustomException(400, "num不能为空");
        }
        int i = 10 / num;
        return "result:" + i;
    }
}

例外処理(キー)

ノート概要

  • @ControllerAdviceの  キャプチャ  Controller あなたが追加した場合の層は、例外をスロー  @ResponseBody リターン情報比較JSON フォーマットを。
  • @RestControllerAdviceは、  に対応する   結合体。@ControllerAdvice@ResponseBody
  • @ExceptionHandler  例外クラスの単一の一種、コードの減少の繰り返し率は、複雑さを軽減します。

作成し  GlobalExceptionHandler たクラスを追加し、  @RestControllerAdvice 注釈は例外通知クラスを定義し、定義されたメソッドに追加することができ  @ExceptionHandler 、異常なキャッチを達成するために...

package com.battcn.config;

import com.battcn.exception.CustomException;
import com.battcn.exception.ErrorResponseEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 全局异常处理
 *
 * @author Levin
 * @since 2018/6/1 0001
 */
@RestControllerAdvice
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {

    /**
     * 定义要捕获的异常 可以多个 @ExceptionHandler({})
     *
     * @param request  request
     * @param e        exception
     * @param response response
     * @return 响应结果
     */
    @ExceptionHandler(CustomException.class)
    public ErrorResponseEntity customExceptionHandler(HttpServletRequest request, final Exception e, HttpServletResponse response) {
        response.setStatus(HttpStatus.BAD_REQUEST.value());
        CustomException exception = (CustomException) e;
        return new ErrorResponseEntity(exception.getCode(), exception.getMessage());
    }

    /**
     * 捕获  RuntimeException 异常
     * TODO  如果你觉得在一个 exceptionHandler 通过  if (e instanceof xxxException) 太麻烦
     * TODO  那么你还可以自己写多个不同的 exceptionHandler 处理不同异常
     *
     * @param request  request
     * @param e        exception
     * @param response response
     * @return 响应结果
     */
    @ExceptionHandler(RuntimeException.class)
    public ErrorResponseEntity runtimeExceptionHandler(HttpServletRequest request, final Exception e, HttpServletResponse response) {
        response.setStatus(HttpStatus.BAD_REQUEST.value());
        RuntimeException exception = (RuntimeException) e;
        return new ErrorResponseEntity(400, exception.getMessage());
    }

    /**
     * 通用的接口映射异常处理方
     */
    @Override
    protected ResponseEntity<Object> handleExceptionInternal(Exception ex, Object body, HttpHeaders headers,
                                                             HttpStatus status, WebRequest request) {
        if (ex instanceof MethodArgumentNotValidException) {
            MethodArgumentNotValidException exception = (MethodArgumentNotValidException) ex;
            return new ResponseEntity<>(new ErrorResponseEntity(status.value(), exception.getBindingResult().getAllErrors().get(0).getDefaultMessage()), status);
        }
        if (ex instanceof MethodArgumentTypeMismatchException) {
            MethodArgumentTypeMismatchException exception = (MethodArgumentTypeMismatchException) ex;
            logger.error("参数转换失败,方法:" + exception.getParameter().getMethod().getName() + ",参数:" + exception.getName()
                    + ",信息:" + exception.getLocalizedMessage());
            return new ResponseEntity<>(new ErrorResponseEntity(status.value(), "参数转换失败"), status);
        }
        return new ResponseEntity<>(new ErrorResponseEntity(status.value(), "参数转换失败"), status);
    }
}

主な機能

package com.battcn;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @author Levin
 */
@SpringBootApplication
public class Chapter17Application {

    public static void main(String[] args) {
        SpringApplication.run(Chapter17Application.class, args);
    }

}

テスト

終了準備が開始されるとChapter17Application、それは以下の試験結果によって見つけることができ、本当に  so easy、コードがきれいになった、拡張性も良い方向に変化しています...

アクセス http:// localhost:8080 / TEST3

{"code":400,"message":"num不能为空"}

アクセス http:// localhost:8080 / test3にNUM = 0?

{"code":400,"message":"/ by zero"}

アクセス http:// localhost:8080 / test3にNUM = 5?

result:2

おすすめ

転載: blog.51cto.com/14230003/2416534