本文转至https://blog.csdn.net/qq_16513911/article/details/86631454
1.Springboot1.x中的处理方式
@Bean
public EmbeddedServletContainerCustomizer containerCustomizer ( ) {
return new EmbeddedServletContainerCustomizer ( ) {
@Override
public void customize ( ConfigurableEmbeddedServletContainer container) {
ErrorPage error401Page = new ErrorPage ( HttpStatus. UNAUTHORIZED, "/401" ) ;
ErrorPage error405Page = new ErrorPage ( HttpStatus. METHOD_NOT_ALLOWED, "/405" ) ;
ErrorPage error404Page = new ErrorPage ( HttpStatus. NOT_FOUND, "/404" ) ;
ErrorPage error500Page = new ErrorPage ( HttpStatus. INTERNAL_SERVER_ERROR, "/500" ) ;
container. addErrorPages ( error401Page, error405Page, error404Page, error500Page) ;
}
} ;
}
2.Springboot2.x 中的处理方式
@Component
public class ErrorConfig implements ErrorPageRegistrar {
@Override
public void registerErrorPages ( ErrorPageRegistry registry) {
ErrorPage error400Page = new ErrorPage ( HttpStatus. BAD_REQUEST, "/error400Page" ) ;
ErrorPage error401Page = new ErrorPage ( HttpStatus. UNAUTHORIZED, "/error401Page" ) ;
ErrorPage error404Page = new ErrorPage ( HttpStatus. NOT_FOUND, "/error404Page" ) ;
ErrorPage error500Page = new ErrorPage ( HttpStatus. INTERNAL_SERVER_ERROR, "/error500Page" ) ;
registry. addErrorPages ( error400Page, error401Page, error404Page, error500Page) ;
}
}