spring boot 使用@ControllerAdvice和@ExceptionHandler实现全局异常管理

package com.core.service.config;

import com.core.service.domain.ResponseData;
import org.springframework.http.HttpStatus;
import org.springframework.web.HttpRequestMethodNotSupportedException;
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.ResponseStatus;
import org.springframework.web.servlet.NoHandlerFoundException;

@ControllerAdvice
@ResponseBody
public class GlobalExceptionConfig {

    /**
     * 页面找不到
     * @param exception
     * @return
     */
    @ResponseStatus(HttpStatus.NOT_FOUND)
    @ExceptionHandler(NoHandlerFoundException.class)
    public ResponseData notFound(NoHandlerFoundException exception) {
        ResponseData responseData = new ResponseData(false,404,exception.getMessage(),null);
        return responseData;
    }

    /**
     * 请求方式不受支持
     * @param exception
     * @return
     */
    @ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public ResponseData methodNotAllowed(HttpRequestMethodNotSupportedException exception) {
        ResponseData responseData = new ResponseData(false,405,exception.getMessage(),null);
        return responseData;
    }


    /**
     * 统一的5xx错误
     * @param exception
     * @return
     */
    @ExceptionHandler(Exception.class)
    public ResponseData defaultException(Exception exception) {
        ResponseData responseData = new ResponseData(false,500,exception.getMessage(),null);
        return responseData;
    }

}

@ControllerAdvice注解的作用:从字面上就可以看出它是标注该类下的所有方法都对@Controller进行方法增强。

@ExceptionHandler注解:表示当出现指定异常时就调用该方法。

猜你喜欢

转载自blog.csdn.net/m0_37224390/article/details/82149131