Three ways: the first type:
Spring Boot maps all errors to/error by default, implementing Errorcontroller
You need to define a controller to implement Errorcontroller.
PackageCom.example.controller;ImportOrg.slf4j.Logger;Importorg.slf4j.LoggerFactory;ImportOrg.springframework.boot.autoconfigure.web.ErrorController;ImportOrg.springframework.stereotype.Controller;Importorg.springframework.web.bind.annotation.RequestMapping; @Controller @requestmapping (Value= "Error") Public classBaseerrorcontrollerImplementsErrorcontroller {Private Static FinalLogger Logger = Loggerfactory.getlogger (Baseerrorcontroller.class); @Override PublicString Geterrorpath () {Logger.info ("Something went wrong!" Enter custom error Controller "); return"Error/error"; } @RequestMapping PublicString error () {returnGeterrorpath (); }}
Also create a new error directory under Resource/templates and add ERROR.FTL so that the ERROR.FTL page is displayed when the exception occurs.
<!DOCTYPE HTML><HTML><HeadLang= "en"> <title>Spring Boot Demo-freemarker</title></Head><Body> <H1>error-system error, please contact background administrator</H1></Body></HTML>
Code directory Structure:
Method Two: Add the custom error page
1 HTML static page: defined under resources/public/error/
such as add 404 page: resources/public/error/404.html page, Chinese note page encoding
2 Template Engine page: defined under templates/error/
If you add 5xx page: TEMPLATES/ERROR/5XX.FTL
Note: templates/error/this priority compared to resources/public/error/high, that is, if the templates below if there is a corresponding exception handling page will display it instead of displaying the error directory of the page.
We removed the Errorcontroller class that we just wrote and made an exception in the Webcontroller class for testing.
PackageCom.example.controller;ImportOrg.slf4j.Logger;Importorg.slf4j.LoggerFactory;ImportOrg.springframework.stereotype.Controller;ImportOrg.springframework.ui.ModelMap;Importorg.springframework.web.bind.annotation.RequestMapping; @Controller @requestmapping (Value= "/web") Public classWebcontroller {Private Static FinalLogger Logger = Loggerfactory.getlogger (Webcontroller.class); @RequestMapping (Value= "Index") PublicString Index (Modelmap map) {Logger.info ("This is controller ..."); Map.put ("title", "Freemarker Hello word"); return"Index";//do not add/,linux to the beginning, there will be errors} @RequestMapping (Value= "Error") PublicString error (Modelmap map) {Throw NewRuntimeException ("Test Exception"); }}
Add 404.html and 5XX.FTL
5XX.FTL:
<!DOCTYPE HTML><HTML><HeadLang= "en"> <title>Spring Boot Demo-freemarker</title></Head><Body> <H1>5xx error-system error, please contact background administrator</H1> <H1>Exception information: ${exception}</H1></Body></HTML>
Method Three: Use annotations @controlleradvice
Customize an exception-handling class in which annotations can be used to intercept exceptions, which can be handled differently for different exceptions
Code:
PackageCom.example.handler;ImportOrg.slf4j.Logger;Importorg.slf4j.LoggerFactory;ImportOrg.springframework.http.HttpStatus;ImportOrg.springframework.web.bind.annotation.ControllerAdvice;ImportOrg.springframework.web.bind.annotation.ExceptionHandler;ImportOrg.springframework.web.bind.annotation.ResponseStatus;ImportOrg.springframework.web.servlet.ModelAndView; @ControllerAdvice Public classBizexceptionhandler {Private Static FinalLogger Logger = Loggerfactory.getlogger (Bizexceptionhandler.class); /*** Unified Exception Handling * *@paramException * Exception *@return */@ExceptionHandler ({runtimeexception.class}) @ResponseStatus (Httpstatus.ok) PublicModelandview processexception (RuntimeException exception) {Logger.info ("Custom Exception Handling-runtimeexception"); Modelandview m=NewModelandview (); M.addobject ("Ericexception", Exception.getmessage ()); M.setviewname ("Error/500"); returnm; } /*** Unified Exception Handling * *@paramException * Exception *@return */@ExceptionHandler ({Exception.class}) @ResponseStatus (Httpstatus.ok) PublicModelandview processexception (Exception Exception) {logger.info ("Custom Exception Handling-exception"); Modelandview m=NewModelandview (); M.addobject ("Ericexception", Exception.getmessage ()); M.setviewname ("Error/500"); returnm; }}
Create a new error page under Templates/error 500.FTL
<!DOCTYPE HTML><HTML><HeadLang= "en"> <title>Spring Boot Demo-freemarker</title></Head><Body> <H1>error-system error, please contact the background administrator</H1> <H1>Exception information: ${ericexception}</H1></Body></HTML>
Code structure:
These three options are available on demand and are recommended for a third, more flexible approach.
Spring Boot Learning (Error handling)