SpringBoot @RestControllerAdvice统一异常处理记录

一、应用场景

@RestControllerAdvice 和 @ControllerAdvice可实现对异常的统一拦截处理,有利于自定义异常返回格式。其中使用前者不再需要@ResponseBody指定,其返回为json格式。

二、实现部分

pom.xml



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.4.RELEASE
         
    
    com.example
    demo
    0.0.1-SNAPSHOT
    Demo
    Demo project for Spring Boot

    
        UTF-8
        1.8
    

    
        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
        
            org.springframework.boot
            spring-boot-devtools
            true
        

        
        
            org.springframework.boot
            spring-boot-starter-data-jpa
        

        
        
            mysql
            mysql-connector-java
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    



UnionExceptionHandler.java 统一异常处理类

需要在类前加上 @RestControllerAdvice
类中通过 @ExceptionHandler(MyException.class) 添加了对MyException异常类(自定义)的捕捉,当然也支持捕捉常见的一些异常

package com.example.exception;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.ui.Model;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
@RestControllerAdvice
public class UnionExceptionHandler {

    /**
     * Description : 全局异常捕捉处理
     * @param ex
     * @return
     */
    @ExceptionHandler(MyException.class)
    public Map errorHandler(MyException ex) {
        Map map = new HashMap();
        map.put("code", ex.getCode());
        map.put("msg", ex.getMessage());
        map.put("success", false);
        return map;
    }
}

MyController.java

package com.example.controller;

import com.example.DO.UserDO;
import com.example.Service.UserService;
import com.example.exception.MyException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
//import org.springframework.web.bind.annotation.RestController;

//@RestController // 该注解使controller中所有返回都为json格式
@Controller
@RequestMapping("")
public class MyController {

     @ResponseBody
    @RequestMapping("/exceptionHandler")
    public String test(){
        if(true)
            throw new MyException(100, "自定义异常");
        return "welcome";
    }

}

MyException.java

package com.example.exception;

public class MyException extends RuntimeException {

    private String msg;
    private int code = 500;

    public MyException(String msg){
        super(msg);
        this.msg = msg;
    }

    public MyException(String msg, Throwable e) {
        super(msg, e);
        this.msg = msg;
    }

    public MyException(int code, String msg) {
        super(msg);
        this.msg = msg;
        this.code = code;
    }

    public MyException(int code, String msg, 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;
    }
}

三、结果展示

请求url: localhost:8080/exceptionHandler
结果:SpringBoot @RestControllerAdvice统一异常处理记录_第1张图片

你可能感兴趣的:(SpringBoot)