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