springboot配置404、403、500等错误页面自动跳转

记录

springboot 1.x可使用(springboot2.x以上版本EmbeddedServletContainerCustomizer已被替代):

import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.web.servlet.ErrorPage;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
 
@Configuration
public class ErrorPageConfig  {
    @Bean
    public EmbeddedServletContainerCustomizer containerCustomizer() {
        return new EmbeddedServletContainerCustomizer() {
            public void customize(ConfigurableEmbeddedServletContainer container) {
                ErrorPage error400Page = new ErrorPage(HttpStatus.BAD_REQUEST, "/error/400.htm");
                ErrorPage error401Page = new ErrorPage(HttpStatus.UNAUTHORIZED, "/error/401.htm");
                ErrorPage error404Page = new ErrorPage(HttpStatus.NOT_FOUND, "/error/404.htm");
                ErrorPage error500Page = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/error/500.htm");
 
                container.addErrorPages(error400Page, error401Page, error404Page, error500Page);
            }
        };
    }
}

或者:

@ServletComponentScan
@SpringBootApplication
public class Application extends SpringBootServletInitializer implements EmbeddedServletContainerCustomizer{

	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}
	
	@Override
	public void customize(ConfigurableEmbeddedServletContainer container) {
		container.addErrorPages(new ErrorPage(HttpStatus.BAD_REQUEST, "/error/400"));            
		container.addErrorPages(new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/error/500"));            
		container.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/error/404"));     
	}
}

springboot 2.x可使用:

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.boot.autoconfigure.web.servlet.error.ErrorViewResolver;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.ModelAndView;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


@Component
public class OpenApiErrorViewResolver implements ErrorViewResolver {
	
	private static Logger log = LoggerFactory.getLogger(OpenApiErrorViewResolver.class);
	
    @Override
    public ModelAndView resolveErrorView(HttpServletRequest request, HttpStatus status, Map model) {
    	log.info("进入异常跳转");
        if("404".equals(String.valueOf(status))){
        	log.info("404异常跳转");
            return new ModelAndView("/error/404");
        }else if("403".equals(String.valueOf(status))){
        	log.info("403异常跳转");
            return new ModelAndView("/error/unauth");
        }else if("500".equals(String.valueOf(status))){
        	log.info("500异常跳转");
            return new ModelAndView("/error/500");
        }else{
        	log.info("默认异常跳转");
            return new ModelAndView("/error/404");
        }
    }
}

或者:

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class MyException implements ErrorController {
 
	private static Logger log = LoggerFactory.getLogger(MyException.class);
 
    @RequestMapping("/error")
    public String handleError(HttpServletRequest request) {
    	log.info("进入异常跳转");
        Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
        switch (statusCode) {
            case 404:
            	log.info("404异常跳转");
                return "/error/404";
            case 403:
            	log.info("403异常跳转");
                return "/error/unauth";
            case 500:
            	log.info("500异常跳转");
                return "/error/500";
            default:
            	log.info("默认异常跳转");
                return "/error/404";
        }
 
    }
    
    @Override
    public String getErrorPath() {
        return "/error";
    }
}

 

你可能感兴趣的:(Spring,Boot)