Spring boot全局捕获异常处理!

package com.htli.util;

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

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

@ControllerAdvice
public class GlobalExceptionHandler {

    /**
     * @ExceptionHandler 表示拦截异常
     *    @ControllerAdvice 是 controller 的一个辅助类,最常用的就是作为全局异常处理的切面类
     *    @ControllerAdvice 可以指定扫描范围
     *    @ControllerAdvice 约定了几种可行的返回值,如果是直接返回 model 类的话,需要使用 @ResponseBody 进行 json 转换
     *    返回 String,表示跳到某个 view
     *    返回 modelAndView
     *    返回 model + @ResponseBody
     */
    /**
     * 返回json字符串的 还有返回modeAndView的
     * @return
     */
    @ExceptionHandler(RuntimeException.class)
    @ResponseBody
    public Map<String, Object> exceptionHandler(){
        Map<String, Object> errorResultMap = new HashMap<String, Object>();
        errorResultMap.put("errorCode", "500");
        errorResultMap.put("errorMsg", "系統错误!");
        return errorResultMap;
    }
    
}

一个捕获异常的工具类

posted @ 2018-09-06 23:28  Java开发小菜鸟  阅读(3604)  评论(0编辑  收藏  举报