Spring boot, as a handy framework for microservices, has some new processing on error page processing, unlike the previous spring MVC
500 of the page processing is relatively simple, in the form of Java Config or XML, the definition of the following beans can be
<bean class= "Org.springframework.web.servlet.handler.SimpleMappingExceptionResolver" > < Property name= "Exceptionmappings" > <props> <prop key= " Org.apache.shiro.authz.UnauthenticatedException ">pages/403</prop> <prop key=" Org.apache.shiro.authz.UnauthorizedException ">pages/403</prop> <prop key=" Org.apache.shiro.authc.LockedAccountException ">pages/locked</prop> <prop key=" Java.lang.Throwable ">pages/500</prop> </props> </property> </bean>
404 is more special, there are 2 ways to refer to:
1.
Set Dispatcherservlet First
@Bean public Servletregistrationbean dispatcherregistration (Dispatcherservlet dispatcherservlet) { Servletregistrationbean registration = new Servletregistrationbean ( dispatcherservlet); Dispatcherservlet.setthrowexceptionifnohandlerfound (true); return registration; }
Add the handler of the processing error page, plus@ControllerAdvice Annotations
@ControllerAdvicepublic class Globalcontrollerexceptionhandler {public static final String Default_error_view = "pages/ 404 "; @ExceptionHandler (value = nohandlerfoundexception.class) public Modelandview Defaulterrorhandler ( HttpServletRequest req, Exception e) throws Exception { Modelandview Mav = new Modelandview (); Mav.addobject ("Exception", e); Mav.addobject ("url", Req.getrequesturl ()); Mav.setviewname (Default_error_view); return MAV;} }
However, the above processing method will result in the filtering of resources such as JS,CSS, preferably using the second method
2. Integrated Errorcontroller
@Controllerpublic class Mainsiteerrorcontroller implements Errorcontroller {private static final String Error_path = "/er Ror "; @RequestMapping (value=error_path) public String HandleError () { return "pages/404"; } @Override public String Geterrorpath () { //TODO auto-generated method stub return error_path;}}
Spring boot under 500 404 error page Processing