springboot自定义异常拦截失败,使用分布式,从gateway转发至指定模块,单独写了一个工具类,其中异常也写在内,通过maven形式将工具类放入至模块,但是未拦截成功,麻烦帮忙看下,谢谢。代码如下
工具类代码
public class CustomException extends RuntimeException{
private static final long serialVersionUID = 440796089575624843L;
public CustomException(String message) {
super(message);
}
@ControllerAdvice
public class GlobalExceptionHandler {
private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);
@ExceptionHandler
@ResponseBody
public Result defaultErrorHandler(Exception e, HttpServletResponse response) {
if (e instanceof CustomException) {
response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
return Result.fail(StatusCode.Error.toString(),e.getMessage());
} else if (e instanceof ValidationException) {
response.setStatus(HttpStatus.NOT_IMPLEMENTED.value());
return Result.fail(StatusCode.Validation_Error.toString(),e.getMessage());
} else {
response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
return Result.fail(StatusCode.Other_Error.toString(),e.getMessage());
}
}
后面就是抛出异常了,控制台输出为
通过apifox调用后未获取异常消息成功,且也不是自己封装的响应
下面这个图是响应成功的实例