Spring Boot Basic Tutorial 9-web application Development-Error handling

Source: Internet
Author: User

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

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.