Springboot Unified Exception Handling

Source: Internet
Author: User
Tags exception handling getmessage

Unified Exception Handling:

@ControllerAdvice Public classGlobalexceptionhandler {PrivateLogger Logger =Loggerfactory.getlogger (GetClass ()); /** * Handle Custom exceptions*/@ExceptionHandler (authexception.class) @ResponseBody PublicR handlerrexception (authexception e) {r R=NewR (); R.put ("Code", E.getcode ()); R.put ("msg", E.getmessage ()); returnR; } @ExceptionHandler (Exception.class) @ResponseBody PublicR handleexception (Exception e) {logger.error (E.getmessage (), E); returnR.error (); }}

Now the internet is generally a relatively simple wording

There are other ways:

 Public classControllerexceptionresolver extends Exceptionhandlerexceptionresolver {Private StaticFinal Logger LOG = Loggerfactory.getlogger (controllerexceptionresolver.class); protectedModelandview doresolvehandlermethodexception (httpservletrequest request, httpservletresponse response, Handlermethod Handlermethod, Exception Exception) {String URI=Request.getrequesturi (); Log.error ("Exception URL:"+ URI +", processing framework", exception); if(Exception instanceof stonesystemruntimeexception) {//TODO attacks or unusual access will occurStonesystemruntimeexception stoneruntimeexception =(stonesystemruntimeexception) exception; Basicres Res=Newbasicres ();            Res.setmsg (Stoneruntimeexception.getmsg ());            Res.setrescode (Stoneruntimeexception.getcode ());  This. Excutejson (response, RES); Log.error ("system Run-time exceptions", exception); } Else if(Exception instanceof stonebizzruntimeexception) {//TODO general Business ExceptionStonebizzruntimeexception stonebizzexception =(stonebizzruntimeexception) exception; Basicres Res=Newbasicres ();            Res.setmsg (Stonebizzexception.getmsg ());            Res.setrescode (Stonebizzexception.getcode ());  This. Excutejson (response, RES); Log.error ("Business Exception", exception); } Else {            //TODO Other unhandled exceptionsBasicres res =Newbasicres (); Res.setmsg ("system Update, please try again later");            Res.setrescode (ErrorCode.SYSTEM_EXCEPTION.getCode ());  This. Excutejson (response, RES); Log.error ("no exception defined", exception); }        return NewModelandview (); }    PrivateString Excutejson (httpservletresponse response, basicres res) {Try {            byte[] Jsonbytes =json.tojsonbytes (RES); String Exjson=NewString (jsonbytes, Sysconstant.charset_utf8); Response.setcontenttype ("Application/json"); Response.setcharacterencoding ("UTF-8");            Response.getwriter (). write (Exjson); returnExjson; } Catch(Exception ex) {ex.printstacktrace (); }        return NULL; }}

Replace default:

@Configuration Public classInterceptorconfig extends Webmvcconfigureradapter {@Override Public voidExtendhandlerexceptionresolvers (listexceptionresolvers)        {super.extendhandlerexceptionresolvers (exceptionresolvers); Exceptionresolvers.add ( This. Getcontrollerexceptionresolver ()); } @Bean Publiccontrollerexceptionresolver Getcontrollerexceptionresolver () {return NewControllerexceptionresolver (); }}

Inheritance implements the Exceptionhandlerexceptionresolver class, which is generally seen in Springmvc, but can still be used in Springboot

http://blog.didispace.com/springbootexception/

Http://www.cnblogs.com/xinzhao/p/4902295.html

https://juejin.im/entry/5a5f3d61f265da3e5537f113

Springboot Unified Exception 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.