使用Spring,我尝试在我的web应用程序中为一个名为关联的对象设置日志记录,如下所示:-
LoggingCorrelationEnrichingAspect.java:-
@Aspect
@Component
public class LoggingCorrelationEnrichingAspect {
private static final Logger logger = getLogger(LoggingCorrelationEnrichingAspect.class);
@Around("@annotation(Correlated)")
public Object wrapWithCorrelationContext(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
logger.info("Entering "+ proceedingJoinPoint.getSignature().getName() +" with Correlation Id:: "
+ ((Map)proceedingJoinPoint.getArgs()[0]).get(CommerceConnectorConstants.HttpHeaders.CORRELATION_ID).get());
return ((Mono<?>) proceedingJoinPoint.proceed());
}
}Correlated.java:-
@Inherited
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Correlated {}在我主要的REST控制器操作中,使用@Correlated注释,我尝试记录这个关联如下:
@Correlated
@GetMapping(path = "/products}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public Mono<ProductBeanResponse> getProducts(
@RequestHeader(name = Test.HttpHeaders.TENANT_ID, required = true) UUID tId,
@RequestHeader(name = Test.HttpHeaders.CORRELATION_ID, required = true) UUID correlationId
----
---
}但是,当我使用PostMan工具测试我的服务并查看应用程序日志时,关联id永远不会被记录:-
logger.info("Entering "+ proceedingJoinPoint.getSignature().getName() +" with Correlation Id:: "
+ ((Map)proceedingJoinPoint.getArgs()[0]).get(CommerceConnectorConstants.HttpHeaders.CORRELATION_ID).get());请告知这是Spring中的配置问题。
谢谢
发布于 2018-10-15 04:59:16
这可以在以下两种方法中的任何一种方式下工作
Correlated的完全限定名提供为@Around("@annotation(com.x.y.z.Correlated)")Correlated作为第二个参数
@ with (“@批注(关联)”)公共对象wrapWithCorrelationContext(ProceedingJoinPoint proceedingJoinPoint,关联)抛出具有关联Id的Throwable { logger.info("Entering "+ proceedingJoinPoint.getSignature().getName() +“”):“+ ((Map)proceedingJoinPoint.getArgs()).get(CommerceConnectorConstants.HttpHeaders.CORRELATION_ID).get());返回(Mono<?>)proceedingJoinPoint.proceed()”};如有其他需要,请在评论中告知。
P.S.:也如M. Deinum指出的那样,确保删除对象强制转换。
https://stackoverflow.com/questions/52639442
复制相似问题