如何使用 Spring AOP 记录 rest 控制器异常?
How to log rest controller exceptions by using Spring AOP?
我有一个休息控制器和 Spring AOP class,它捕获服务级别自定义异常并记录它们,但我还需要从控制器端记录异常。
这是我的 Spring AOP class;
@Aspect
@Component
public class ExceptionLoggerAspect {
@AfterThrowing(pointcut = "execution(* com.b.m.service.GaService.*(..)) ", throwing = "ex")
public void logError(JoinPoint joinPoint, GaException ex) {
Signature signature = joinPoint.getSignature();
String methodName = signature.getName();
String stuff = signature.toString();
String arguments = Arrays.toString(joinPoint.getArgs());
LOGGER.error("Exception have been caught in method:\n "
+ "[" + methodName + "] with arguments "
+ arguments + " and the full toString: [" + stuff + "] \n the exception is: ["
+ ex.type.getMessage() + " - " + ex.type.getAdditionalInfo() + "]");
}
}
如何在此处添加我的控制器 class 以记录异常?
我用“execution”切入点添加了我的控制器包路径,但它不起作用。
从 Spring 3.2 开始,您可以使用 @ControllerAdvice
,这使我们能够将之前分散的多个 @ExceptionHandlers
整合到一个单一的全局错误处理组件中。这也将使我们能够完全控制响应的主体以及状态代码。
@ControllerAdvice
public class GlobalControllerExceptionHandler extends ResponseEntityExceptionHandler {
@ExceptionHandler(value = {Exception.class})
public ResponseEntity<Object> handleGenericExceptions(Exception exception, WebRequest webRequest) {
log.error("Handling: ", exception);
HttpStatus errorCode = HttpStatus.INTERNAL_SERVER_ERROR;
return this.handleExceptionInternal(exception, "Unexpected Internal Server Error", new HttpHeaders(), errorCode, webRequest);
}
}
使用@ControllerAdivce
@Slf4j
@ControllerAdvice
@RestController
public class GlobalExceptionHandler {
@ExceptionHandler(Exception.class)
public final ResponseEntity<Object> handleAllExceptions(Exception ex) {
log.error("Exception occurred", ex);
ErrorResponse errorResponse = ErrorResponse.builder().code(Error.UNKNOWN_ERROR.getCode())
.message(Error.UNKNOWN_ERROR.getMessage()).build();
return new ResponseEntity<>(errorResponse, Error.UNKNOWN_ERROR.getHttpStatus());
}
}
使用错误响应 Bean 发送其余响应。
public class ErrorResponse implements Serializable {
private static final long serialVersionUID = -2405172041950251807L;
private Integer code;
private String message;
}
我有一个休息控制器和 Spring AOP class,它捕获服务级别自定义异常并记录它们,但我还需要从控制器端记录异常。
这是我的 Spring AOP class;
@Aspect
@Component
public class ExceptionLoggerAspect {
@AfterThrowing(pointcut = "execution(* com.b.m.service.GaService.*(..)) ", throwing = "ex")
public void logError(JoinPoint joinPoint, GaException ex) {
Signature signature = joinPoint.getSignature();
String methodName = signature.getName();
String stuff = signature.toString();
String arguments = Arrays.toString(joinPoint.getArgs());
LOGGER.error("Exception have been caught in method:\n "
+ "[" + methodName + "] with arguments "
+ arguments + " and the full toString: [" + stuff + "] \n the exception is: ["
+ ex.type.getMessage() + " - " + ex.type.getAdditionalInfo() + "]");
}
}
如何在此处添加我的控制器 class 以记录异常? 我用“execution”切入点添加了我的控制器包路径,但它不起作用。
从 Spring 3.2 开始,您可以使用 @ControllerAdvice
,这使我们能够将之前分散的多个 @ExceptionHandlers
整合到一个单一的全局错误处理组件中。这也将使我们能够完全控制响应的主体以及状态代码。
@ControllerAdvice
public class GlobalControllerExceptionHandler extends ResponseEntityExceptionHandler {
@ExceptionHandler(value = {Exception.class})
public ResponseEntity<Object> handleGenericExceptions(Exception exception, WebRequest webRequest) {
log.error("Handling: ", exception);
HttpStatus errorCode = HttpStatus.INTERNAL_SERVER_ERROR;
return this.handleExceptionInternal(exception, "Unexpected Internal Server Error", new HttpHeaders(), errorCode, webRequest);
}
}
使用@ControllerAdivce
@Slf4j
@ControllerAdvice
@RestController
public class GlobalExceptionHandler {
@ExceptionHandler(Exception.class)
public final ResponseEntity<Object> handleAllExceptions(Exception ex) {
log.error("Exception occurred", ex);
ErrorResponse errorResponse = ErrorResponse.builder().code(Error.UNKNOWN_ERROR.getCode())
.message(Error.UNKNOWN_ERROR.getMessage()).build();
return new ResponseEntity<>(errorResponse, Error.UNKNOWN_ERROR.getHttpStatus());
}
}
使用错误响应 Bean 发送其余响应。
public class ErrorResponse implements Serializable {
private static final long serialVersionUID = -2405172041950251807L;
private Integer code;
private String message;
}