spring boot 全剧异常处理

package com.study.exception;

import org.springframework.boot.web.server.ConfigurableWebServerFactory;
import org.springframework.boot.web.server.ErrorPage;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@ControllerAdvice
public class GlobalException {

    @ExceptionHandler(value=RuntimeException.class)
    public @ResponseBody String defaultErrorHandler(HttpServletRequest request,Exception e){
        e.printStackTrace();
        return "我是异常处理类!";
    }

    @Bean
    public WebServerFactoryCustomizer webServerFactoryWebServerFactoryCustomizer(){
        return (factory -> {
            ErrorPage errorPage = new ErrorPage(HttpStatus.NOT_FOUND,"/404");
            factory.addErrorPages(errorPage);
        });

    }

}
@ExceptionHandler(value=RuntimeException.class) 全局异常处理返回 
我是异常处理类 字符串
WebServerFactoryCustomizer 全局异常默认页面设置

 

 

 

 

你可能感兴趣的:(spring boot 全剧异常处理)