天天看點

Springboot 自定義異常和全局異常資訊的處理

        在程式中,我們往往需要對異常的級别做處理,做資料記錄,或者發送郵件預警處理,接下來我們在springboot程式中,實作自定義的異常處理和全局異常處理。

      首先是自定義異常,我們自定義一個類,定義你需要傳回的 code 和 message,需要更多的處理資訊可以自行追加

package com.hqk.bootdemo.util;

/**
 * <p>自定義異常處理資訊</p >
 *
 * @author hqk
 * @version 1.0: MyException.java v0.1 2019/6/19 上午10:55 hqk Exp$
 */
public class MyException extends RuntimeException{

    //異常狀态碼
    private String code;

    //異常資訊
    private String message;


    public MyException(String code, String message) {
        this.code = code;
        this.message = message;
    }

    public String getCode() {
        return code;
    }

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

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}
           

在springboot中,我們可以使用  @ControllerAdvice 或者 @RestControllerAdvice 注解來處理異常,前者需要加上 @ResponseBody 才能傳回 json 資料 

package com.hqk.bootdemo.util;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

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

/**
 * <p>異常處理</p >
 *
 * @author hqk
 * @version 1.0: AllControllerAdvice.java v0.1 2019/6/19 上午10:48 hqk Exp$
 */
@RestControllerAdvice
public class AllControllerAdvice {


    /**
     * 自定義異常處理
     * @param e
     * @return
     */
    @ExceptionHandler(value = MyException.class)
    public Map<String,Object> customexceptionHandler(MyException e){

        Map<String,Object> map  = new HashMap<String,Object>();

        map.put("code",e.getCode());
        map.put("msg",e.getMessage());
        map.put("data",null);

        //可以對異常進行日志記錄,寫入資料庫 或者 發送郵件預警
        // if(e.getCode().equals("110")){ 寫入資料庫 或者 發送郵件預警  }
        // log.info("錯誤資訊"+e.getMessage());

        return map;
    }


    /**
     * 全局異常處理
     * @param e
     * @return
     */
    @ExceptionHandler(value = Exception.class)
    public Map<String,Object> exceptionHandler(Exception e){

        Map<String,Object> map  = new HashMap<String,Object>();

        map.put("code","01");
        map.put("msg",e.getMessage());
        map.put("data",null);

        return map;
    }
}
           

接下來,我們寫個 controller 進行測試 自定義異常 和 全局異常

package com.hqk.bootdemo.controller;

import com.hqk.bootdemo.util.MyException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * <p></p >
 *
 * @author hqk
 * @version 1.0: HelloController.java v0.1 2019/6/18 下午3:54 hqk Exp$
 */
@RestController
@RequestMapping("/app/system")
public class HelloController {


    @RequestMapping("version")
    public Map<String, Object> getHello() {

        Map<String, Object> map = new HashMap<>();

        map.put("code", "00");
        map.put("msg", "請求成功");

        try {

            //抛異常
            System.out.println(1 / 0);

        } catch (Exception e) {

            //抛出自定義異常資訊
            throw new MyException("110", "這是個重大的bug,請馬上處理");
        }


        return map;

    }


    @RequestMapping("hello")
    public Map<String, Object> getHai() {

        Map<String, Object> map = new HashMap<>();

        map.put("code", "00");
        map.put("msg", "請求成功");
        
        System.out.println(1 / 0);

        return map;

    }



}
           

接下來我們打開浏覽器進行測試,如圖,分别列印自定義異常資訊 和全局異常資訊

Springboot 自定義異常和全局異常資訊的處理
Springboot 自定義異常和全局異常資訊的處理

繼續閱讀