springboot 默认异常处理

之前一直在异常处理这方面不咋用心,钻研了一下,原来SpringBoot默认有自定义异常处理的体系,在做SpringBoot项目的时候,如果是抛出了运行时异常,springBoot并会对异常进行处理,返回如下异常信息:

    {  
    "timestamp": 1517294278132,  
    "status": 500,  
    "error": "Internal Server Error",  
    "exception": "com.lgy.common.exception.BusinessException",  
    "message": "[001]自定义的uncheck 异常!",  
    "path": "/validateExceptionTest"  


当SpirngBoot出现异常信息时候,会默认访问/error,springBoot种有BasicErrorController这个类来处理异常信息: 


springboot 默认异常处理

springboot 默认异常处理


如果要取代SpringBoot默认的异常处理信息方式,继承ErrorController:

springboot 默认异常处理

接着在访问,并是自定义处理的异常信息了:

{  
    "message": "[001]自定义的uncheck 异常!",  
    "path_": "/validateExceptionTest",  
    "status_": 500