背景:
依赖包中有一个全局异常处理,这个全局异常处理对Exception异常进行了处理,并返回内置的错误提示信息,产品经理觉得这个提示信息不够友好,需要改写提示信息。
解决方法:
如果自定义全局异常处理类,对Exception异常进行处理,启动时会报错,提示有两个全局异常处理类对Exception异常进行了处理。此时可以考虑使用AOP来重写依赖包中的全局异常处理。方法如下,废话不多说,直接上代码:
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
@Aspect
@Component
@slf4j
public class GlobalExceptionHandlerAspect {
@Pointcut("execution (*com.gongmin.GlobalExceptionHandler.exceptionHandler(..))")
public void exceptionHandlerCut() {
}
@Around("exceptionHandlerCut()")
public Object doAround(ProceedingJoinPoint joinPoint) {
Response response = new Response()
response.setCode(500):
response.setMsg("服务器现在太忙了,请稍后重试!!!");
return response;
}
}