Spring Boot error handling mechanism

Source: Internet
Author: User
Tags error handling

When the request method is not present

The browser appears with a white label error page

Other clients appear in response to a JSON-formatted text that contains information such as an error code

The browser sends the requested header:

Client Request Header

So that we can distinguish from which.

Principle: Errormvcautoconfiguration automatic configuration for handling errors

The following components are added inside

Defaulterrorattributes
help us share information on the page;    @Override  Public Map<string, object> geterrorattributes (Requestattributes requestattributes,            Boolean Includestacktrace) {        Mapnew linkedhashmap<string, object>();        Errorattributes.put ("timestamp"new  Date ());        Addstatus (Errorattributes, requestattributes);        Adderrordetails (Errorattributes, Requestattributes, includestacktrace);        Addpath (Errorattributes, requestattributes);         return errorattributes;    }

Basicerrorcontroller Processing/error Requests
@Controller @requestmapping ("${server.error.path:${error.path:/error}}") Public classBasicerrorcontroller extends Abstracterrorcontroller {@RequestMapping (produces="text/html")//Generate HTML-type data; The browser sends a request to this method to process     PublicModelandview errorHtml (httpservletrequest request, httpservletresponse response) {Httpstatus Statu S=GetStatus (Request); Map<string, object> model =Collections.unmodifiablemap (geterrorattributes (Request, Isincludestacktrace (request, Mediatype.text_h        (TML)));                Response.setstatus (Status.value ()); //which page to go to as error page, including page address and page contentModelandview Modelandview =Resolveerrorview (Request, response, status, model); return(Modelandview = =NULL?NewModelandview ("Error", model): Modelandview); }

@RequestMapping @ResponseBody//generate JSON data, other clients come to this method to handle; PublicResponseentity<map<string, object>>error (HttpServletRequest request) {Map<string, object> BODY =geterrorattributes (Request, Isincludestacktrace (request, Mediatype.all)); Httpstatus Status=GetStatus (Request); return NewResponseentity<map<string, object>>(body, status); }

Errorpagecustomizer (Error 4xx,5xx such as system error) came to/error request
@Value ("${error.path:/error}")    private"/error ";  After the system error occurs, the error request is processed; (Web. XML registered page rules for errors)

Defaulterrorviewresolver
@Override PublicModelandview Resolveerrorview (httpservletrequest request, httpstatus status, Map<string, object>model) {Modelandview Modelandview=Resolve (string.valueof (status), model); if(Modelandview = =NULL&&Series_views.containskey (Status.series ())) {Modelandview= Resolve (series_views.Get(Status.series ()), model); }        returnModelandview; }    PrivateModelandview Resolve (String viewName, map<string, object>model) {        //default Springboot can go to find a page? error/404String Errorviewname ="error/"+ViewName; //the template engine resolves this page address by using the template engine to parseTemplateavailabilityprovider Provider = This. Templateavailabilityproviders. Getprovider (Errorviewname, This. ApplicationContext); if(Provider! =NULL) {            //returns the specified view address to Errorviewname when the template engine is available            return NewModelandview (errorviewname, model); }        //The template engine is not available, just find the Errorviewname page under the static Resources folder error/404.html        returnResolveResource (errorviewname, model); }

Steps:

? Once the system appears 4xx or 5xx error, Errorpagecustomizer will take effect (custom error response rule); will come to/error request; it will be basicerrorcontroller . ;

? 1) The response page, which page is to be parsed by defaulterrorviewresolver ;

protectedModelandview Resolveerrorview (httpservletrequest request, httpservletresponse response, httpstatus status, Map< /c13><string, object>model) {    //all the errorviewresolver get Modelandview    for(Errorviewresolver resolver: This. Errorviewresolvers) {Modelandview Modelandview=Resolver.resolveerrorview (Request, status, model); if(Modelandview! =NULL) {         returnModelandview; }   }   return NULL;}

Spring Boot error handling mechanism

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.