1、概要
「に基づき、springboot -のErrorControllerにカスタマイズし、実装するためのコントローラのマッピング/エラー」次のように改革を、唯一MyCustomErrorControllerクラスが変更されます。
輸入org.springframework.beans.factory.annotation.Autowired; 輸入org.springframework.boot.web.servlet.error.ErrorAttributes。 輸入org.springframework.boot.web.servlet.error.ErrorController; 輸入org.springframework.stereotype.Controller; 輸入org.springframework.web.bind.annotation.RequestMapping。 輸入org.springframework.web.bind.annotation.ResponseBody。 輸入org.springframework.web.context.request.ServletWebRequest; インポートのjavax.servlet.http.HttpServletRequest; 輸入java.util.Map; / ** * @author www.gomepay.com * @date 2019年11月18日 * / @Controller パブリック クラス MyCustomErrorControllerの実装のErrorController { @Autowired プライベートErrorAttributes errorAttributes。 (@RequestMapping "/エラー" ) @ResponseBody パブリック文字列handleErrorの(HttpServletRequestのリクエスト){ ServletWebRequest servletWebRequest = 新しいServletWebRequest(要求)。 地図 <文字列、オブジェクト> errorAttributes = この .errorAttributes.getErrorAttributes(servletWebRequest、真の); 最終 StringBuilderのerrorDetails = 新しいですStringBuilder(); errorAttributes.forEach((属性、値) - > { errorDetails.append( "<TR> <TD>" ) .append(属性) .append( "</ TD> <TD> <PRE>" ) .append(値) .append( "</ PRE> </ TD> </ TR>" ); }); 戻り String.Formatの( "<HTML> <HEAD> <スタイル> TD {垂直整列:トップ;ボーダー:固体1ピクセル#666;} </スタイル>" +「</ HEAD> <BODY> <H2>エラーページ</ H2> <TABLE>%S </ TABLE> </ body> </ HTML> ストリングgetErrorPath(){ リターン "/エラー" 。 } }
実行:
ErrorControllerで使用ErrorAttributes私たちのカスタム、あなたはerror.jsp 404.jspページを置き換えることができます。