1. 程式人生 > >Springboot2.0全域性異常捕捉處理和自定義全域性異常處理

Springboot2.0全域性異常捕捉處理和自定義全域性異常處理

一,全域性異常捕捉處理

新建MyControllerAdvice類,建在包下都有作用:

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.HashMap;
import java.util.Map;

/**
 * @Auther
: cookie * @Date: 2018/7/26 15:09 * @Description: 全域性捕獲異常和自定義全域性捕獲異常 */
@ControllerAdvice //不指定包預設加了@Controller和@RestController都能控制 //@ControllerAdvice(basePackages ="com.example.demo.controller") public class MyControllerAdvice { /** * 全域性異常處理,反正異常返回統一格式的map * @param ex * @return */
@ResponseBody @ExceptionHandler(value = Exception.class) public Map<String,Object> exceptionHandler(Exception ex){ Map<String,Object> map = new HashMap<String,Object>(); map.put("code",1001); map.put("mag",ex.getMessage()); //發生異常進行日誌記錄,寫入資料庫或者其他處理,此處省略
return map; } }

測試:
controller下的方法:

 @RequestMapping("/{id}")
      public String test(@PathVariable Integer id){
        if(true){
          id=1/id;
        }
      return "success";
      }

結果:
這裡寫圖片描述

id為0時,1除以0異常。

攔截捕捉自定義異常 MyException.class

我的異常類:

/**
 * @Auther: cookie
 * @Date: 2018/7/26 15:22
 * @Description:
 */
public class MyException extends RuntimeException{

    private String code;  //異常狀態碼

    private String message;  //異常資訊

    private String method;   //發生的方法,位置等

    private String descinfo;   //描述

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

    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;
    }

    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public String getDescinfo() {
        return descinfo;
    }

    public void setDescinfo(String descinfo) {
        this.descinfo = descinfo;
    }
}
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.HashMap;
import java.util.Map;

/**
 * @Auther: cookie
 * @Date: 2018/7/26 15:09
 * @Description: 全域性捕獲異常和自定義全域性捕獲異常
 */
@ControllerAdvice  //不指定包預設加了@Controller和@RestController都能控制
//@ControllerAdvice(basePackages ="com.example.demo.controller")
public class MyControllerAdvice {

    /**
     * 攔截捕捉自定義異常 MyException.class
     * @param myex
     * @return
     */
    @ResponseBody
    @ExceptionHandler(value = MyException.class)
    public Map<String,Object> myExceptionHandler(MyException myex){
        Map<String,Object> map  = new HashMap<String,Object>();
        map.put("code",myex.getCode());
        map.put("message",myex.getMessage());
        map.put("method",myex.getMethod());
        map.put("descinfo",myex.getDescinfo());
        //發生異常進行日誌記錄,寫入資料庫或者其他處理,此處省略
        return map;
    }



}

測試:

@RequestMapping(value = "/")
      public String index() throws Exception{
       String name =  redisUtil.set("key100", "666");
       if(StringUtils.isEmpty(name)){
         throw new MyException("1001","empty","/API/getUserName","在獲取使用者名稱字的時候為空");
       }
           return name;
      }

結果:

這裡寫圖片描述