SpringBoot實戰(十):統一異常處理

強烈推薦一個大神的人工智能的教程:http://www.captainbed.net/zhanghan

【前言】

       處理好異常對系統有很好的保護作用同時會大大提高用戶的體驗,對異常統一處理是一個非常實用的技巧,接下來介紹一下如何在系統中對異常進行統一處理;

【統一異常處理】

         一、未加統一異常處理時

               啓動程序,並訪問http://localhost:8080/swagger-ui.html#/,中的lombok演示爲例返回結果如下圖所示,可見返回值是十分的不友好。

         二、進行改造:

               增加全局異常類:

/*
 * Copyright (c) 2019. [email protected] All Rights Reserved.
 * 項目名稱:實戰SpringBoot
 * 類名稱:GlobalExceptionHandler.java
 * 創建人:張晗
 * 聯繫方式:[email protected]
 * 開源地址: https://github.com/dangnianchuntian/springboot
 * 博客地址: https://blog.csdn.net/zhanghan18333611647
 */

package com.zhanghan.zhboot.filter;


import com.zhanghan.zhboot.util.wrapper.WrapMapper;
import com.zhanghan.zhboot.util.wrapper.Wrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.convert.ConversionFailedException;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.validation.BindException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局的的異常攔截器
 *
 * @author https://blog.csdn.net/zhanghan18333611647
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * IllegalArgumentException
     *
     * @param e the e
     * @return the wrapper
     */
    @ExceptionHandler(IllegalArgumentException.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public Wrapper illegalArgumentException(IllegalArgumentException e) {
        log.error("IllegalArgumentException={}", e.getMessage(), e);
        return WrapMapper.error("param error");
    }

    /**
     * IllegalStateException
     *
     * @param e the e
     * @return the wrapper
     */
    @ExceptionHandler(IllegalStateException.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public Wrapper IllegalStateException(IllegalStateException e) {
        log.error("IllegalStateException={}", e.getMessage(), e);
        return WrapMapper.error();
    }

    /**
     * HttpMessageNotWritableException
     *
     * @param e the e
     * @return the wrapper
     */
    @ExceptionHandler(HttpMessageNotWritableException.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public Wrapper HttpMessageNotWritableException(HttpMessageNotWritableException e) {
        log.error("HttpMessageNotWritableException={}", e.getMessage(), e);
        return WrapMapper.error();
    }


    /**
     * HttpMessageNotReadableException
     *
     * @param e the e
     * @return the wrapper
     */
    @ExceptionHandler(HttpMessageNotReadableException.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public Wrapper HttpMessageNotReadableException(HttpMessageNotReadableException e) {
        log.error("HttpMessageNotReadableException={}", e.getMessage(), e);
        return WrapMapper.error();
    }

    /**
     * ConversionFailedException
     *
     * @param e the e
     * @return the wrapper
     */
    @ExceptionHandler(ConversionFailedException.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public Wrapper ConversionFailedException(ConversionFailedException e) {
        log.error("ConversionFailedException={}", e.getMessage(), e);
        return WrapMapper.error();
    }

    /**
     * BindException
     *
     * @param e the e
     * @return the wrapper
     */
    @ExceptionHandler(BindException.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public Wrapper BindException(BindException e) {
        log.error("BindException={}", e.getMessage(), e);
        return WrapMapper.error();
    }

    /**
     * MethodArgumentNotValidException
     *
     * @param e the e
     * @return the wrapper
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public Wrapper validationError(MethodArgumentNotValidException e) {
        log.error("MethodArgumentNotValidException={}", e.getMessage(), e);
        return WrapMapper.error();
    }


    /**
     * Exception
     *
     * @param e the e
     * @return the wrapper
     */
    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public Wrapper exception(Exception e) {
        log.error("Exception={}", e.getMessage(), e);
        return WrapMapper.error();
    }
}

         三、改造後的效果:

         四、項目地址及代碼版本:

               1、地址:https://github.com/dangnianchuntian/springboot

               2、代碼版本:1.3.0-Release

【總結】

         1、提高用戶的體驗度;

         2、統一處理大大減少開發的維護工作。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章