SpringBoot自定义错误页面

@Component
publicclassErrorPageConfigimplementsErrorPageRegistrar{
@Override
publicvoidregisterErrorPages(ErrorPageRegistryregistry){
ErrorPageerror400Page=newErrorPage(HttpStatus.BAD_REQUEST,&
@RequestMapping(&
@Override
publicObjectapplyBeanPostProcessorsBeforeInitialization(ObjectexistingBean,StringbeanName)
throwsBeansException{
Objectresult=existingBean;
for(BeanPostProcessorprocessor:getBeanPostProcessors()){
Objectcurrent=processor.postProcessBeforeInitialization(result,beanName);
if(current==null){
returnresult;
}
result=current;
}
returnresult;
}
@Override
publicObjectpostProcessBeforeInitialization(Objectbean,StringbeanName)throwsBeansException{
if(beaninstanceofErrorPageRegistry){
postProcessBeforeInitialization((ErrorPageRegistry)bean);
}
returnbean;
}
privatevoidpostProcessBeforeInitialization(ErrorPageRegistryregistry){
for(ErrorPageRegistrarregistrar:getRegistrars()){
registrar.registerErrorPages(registry);
}
}
privateCollection<ErrorPageRegistrar>getRegistrars(){
if(this.registrars==null){
//Lookupdoesnotincludetheparentcontext
this.registrars=newArrayList<>(
this.beanFactory.getBeansOfType(ErrorPageRegistrar.class,false,false).values());
this.registrars.sort(AnnotationAwareOrderComparator.INSTANCE);
this.registrars=Collections.unmodifiableList(this.registrars);
}
returnthis.registrars;
}
@Override
publicvoidaddErrorPages(ErrorPage…errorPages){
Assert.notNull(errorPages,&
for(ErrorPageerrorPage:getErrorPages()){
org.apache.tomcat.util.descriptor.web.ErrorPagetomcatErrorPage=neworg.apache.tomcat.util.descriptor.web.ErrorPage();
tomcatErrorPage.setLocation(errorPage.getPath());
tomcatErrorPage.setErrorCode(errorPage.getStatusCode());
tomcatErrorPage.setExceptionType(errorPage.getExceptionName());
context.addErrorPage(tomcatErrorPage);
}
【SpringBoot自定义错误页面】@Override
publicvoidaddErrorPage(ErrorPageerrorPage){
//Validatetheinputparameters
if(errorPage==null)
thrownewIllegalArgumentException
(sm.getString(&
publicvoidadd(ErrorPageerrorPage){
StringexceptionType=errorPage.getExceptionType();
if(exceptionType==null){
statusPages.put(Integer.valueOf(errorPage.getErrorCode()),errorPage);
}else{
exceptionPages.put(exceptionType,errorPage);
}
}
privatevoidstatus(Requestrequest,Responseresponse){
intstatusCode=response.getStatus();
//Handleacustomerrorpageforthisstatuscode
Contextcontext=request.getContext();
if(context==null){
return;
}
/*OnlylookforerrorpageswhenisError()isset.
*isError()issetwhenresponse.sendError()isinvoked.This
*allowscustomerrorpageswithoutrelyingondefaultfrom
*web.xml.
*/
if(!response.isError()){
return;
}
//这里会从errorPageSupport.find(errorCode)获取到错误页
//根据错误码,比如404从statusPages获取对应的ErrorPage对象
ErrorPageerrorPage=context.findErrorPage(statusCode);
if(errorPage==null){
//Lookforadefaulterrorpage
errorPage=context.findErrorPage(0);
}
if(errorPage!=null&&response.isErrorReportRequired()){
response.setAppCommitted(false);
request.setAttribute(RequestDispatcher.ERROR_STATUS_CODE,
Integer.valueOf(statusCode));
Stringmessage=response.getMessage();
if(message==null){
message=&
以上就是朝夕生活(www.30zx.com)关于“SpringBoot自定义错误页面”的详细内容,希望对大家有所帮助!

猜你喜欢