A. handling of Errors
Method One: Spring Boot maps all errors to/error by default, implementing Errorcontroller
@Controller
@RequestMapping (value = "Error")
public class Baseerrorcontroller implements Errorcontroller {
private static final Logger Logger = Loggerfactory.getlogger (Baseerrorcontroller.class);
@Override
Public String Geterrorpath () {
logger.info (" wrong! Enter the custom error Controller ");
return "Error/error";
}
@RequestMapping
Public String error () {
return Geterrorpath ();
}
}
Method Two: Add the custom error page
2.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.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
Method Three: Use annotations @controlleradvice
/**
* Unified Exception Handling
*
* @param exception
* exception
* @return
*/
@ExceptionHandler ({runtimeexception.class})
@ResponseStatus (Httpstatus.ok)
Public Modelandview processexception (RuntimeException exception) {
logger.info (" Custom exception Handling-runtimeexception");
Modelandview m = new Modelandview ();
M.addobject ("Roncooexception", Exception.getmessage ());
M.setviewname ("error/500");
return m;
}
/**
* Unified Exception Handling
*
* @param exception
* exception
* @return
*/
@ExceptionHandler ({exception.class})
@ResponseStatus (Httpstatus.ok)
Public Modelandview processexception (Exception Exception) {
logger.info (" Custom exception Handling-exception");
Modelandview m = new Modelandview ();
M.addobject ("Roncooexception", Exception.getmessage ());
M.setviewname ("error/500");
return m;
}
Concern:
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/8A/57/wKiom1gtgyLjopn6AABtN-e5hXo294.jpg "title=" Dragon Fruit Academy qr code. JPG "alt=" wkiom1gtgyljopn6aabtn-e5hxo294.jpg "/>
Spring Boot Basic Tutorial 9-web application Development-Error handling