SpringBoot 全局异常处理

SpringBoot 抛出全部异常统一进行处理 从而可以自定义返回参数及异常处理

配置文件定义配置项,抛出全部异常

  • yml文件
spring:

  #出现错误时, 直接抛出异常(便于异常统一处理,否则捕获不到404)
  mvc:
    throw-exception-if-no-handler-found: true

  #不要为我们工程中的资源文件建立映射
  resources:
    add-mappings: false
  • properties文件
spring.mvc.throw-exception-if-no-handler-found=true
spring.resources.add-mappings=false

定义全局异常处理器

import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;
import p.u.sys.util.R;

/**
 * 
* @ClassName: GlobalExceptionHandler 
* @Description: 全局异常处理器
* @Author Tao
*  
*  文明观码 神奇 勿动   (Alt+F4 解决一切bug)
*
 */
@RestControllerAdvice
public class GlobalExceptionHandler{

    /**
     * 处理自定义异常
     */
    @ExceptionHandler(CustomException.class)
    public R handleRRException(CustomException e){
        return R.error(e.getCode(),e.getMessage());
    }   
    @ExceptionHandler(NoHandlerFoundException.class)
    public R handleException(NoHandlerFoundException e){
        return R.error(400,e.getMessage());
    }
    @ExceptionHandler(MissingServletRequestParameterException.class)
    public R handleException(MissingServletRequestParameterException e){
        return R.error(999,e.getMessage());
    }
    @ExceptionHandler(Exception.class)
    public R handleException(Exception e){
        return R.error();
    }
}

自定义异常类

/**
 * 
* @ClassName: CustomException 
* @Description: 自定义异常类
* @Author Tao
*  
*  文明观码 神奇 勿动   (Alt+F4 解决一切bug)
*
 */
public class CustomException extends RuntimeException {
    private static final long serialVersionUID = 1L;
    
    private String msg;
    private int code = 500;
    
    public CustomException(String msg) {
        super(msg);
        this.msg = msg;
    }
    
    public CustomException(String msg, Throwable e) {
        super(msg, e);
        this.msg = msg;
    }
    
    public CustomException(String msg, int code) {
        super(msg);
        this.msg = msg;
        this.code = code;
    }
    
    public CustomException(String msg, int code, Throwable e) {
        super(msg, e);
        this.msg = msg;
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public int getCode() {
        return code;
    }

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

返回数据封装类

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

/**
* @ClassName: R 
* @Description: 返回数据封装
* @Author Tao
*  
*  文明观码 神奇 勿动   (Alt+F4 解决一切bug)
*
 */
public class R extends HashMap<String, Object> {
    private static final long serialVersionUID = 1L;
    
    public R() {
        put("code", 200);
        put("msg", "success");
    }
    
    public static R error() {
        return error(500, "服务异常,请联系管理员");
    }
    
    public static R error(String msg) {
        return error(500, msg);
    }
    
    public static R error(int code, String msg) {
        R r = new R();
        r.put("code", code);
        r.put("msg", msg);
        return r;
    }

    public static R ok(String msg) {
        R r = new R();
        r.put("msg", msg);
        return r;
    }
    
    public static R ok(Map<String, Object> map) {
        R r = new R();
        r.putAll(map);
        return r;
    }
    
    public static R ok() {
        return new R();
    }

    @Override
    public R put(String key, Object value) {
        super.put(key, value);
        return this;
    }
}

启动项目后访问 一个错误路径 程序抛出异常 GlobalExceptionHandler 捕获异常 NoHandlerFoundException 并处理返回参数

{
    "msg":"No handler found for GET /universal_api/q",
    "code":400
}

发表留言

人生在世,错别字在所难免,无需纠正。

icon_mrgreen.gificon_neutral.gificon_twisted.gificon_arrow.gificon_eek.gificon_smile.gificon_confused.gificon_cool.gificon_evil.gificon_biggrin.gificon_idea.gificon_redface.gificon_razz.gificon_rolleyes.gificon_wink.gificon_cry.gificon_surprised.gificon_lol.gificon_mad.gificon_sad.gificon_exclaim.gificon_question.gif