Spring boot under 500 404 error page Processing

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.