| package com.dy.common.webUtil; | 
|   | 
| import lombok.extern.slf4j.Slf4j; | 
| import org.springframework.web.bind.annotation.ControllerAdvice; | 
| import org.springframework.web.bind.annotation.ExceptionHandler; | 
| import org.springframework.web.bind.annotation.ResponseBody; | 
|   | 
| /** | 
|  * 全局异常处理,处理在Controller中抛出的异常,在Controller之前(如Fileter)发生的错误或异常由GlErrorCtrl处理 | 
|  * 此类是基于拦截器和实现指定接口(GlExceptionHandler和GlExceptionHandlerImpl) | 
|  * 另一种实现方式是基于注解切面实现(GlExceptionAspect) | 
|  */ | 
| @ControllerAdvice | 
| @ResponseBody | 
| @Slf4j | 
| public class GlExceptionHandler { | 
|   | 
|     @ExceptionHandler | 
|     public BaseResponse GlExceptionHandler(GlException e){ | 
|         log.error("捕获到GlException:{}", e.getMsg(), e); | 
|         return BaseResponseUtils.buildException(e.getMsg()); | 
|     } | 
|   | 
|     public BaseResponse exceptionHandler(Throwable t) { | 
|         log.error("捕获到异常:{}",t.getMessage(),t); | 
|         return BaseResponseUtils.buildException("在Controller中或后产生异常:" + t.getMessage()); | 
|     } | 
| } |