gpt4 book ai didi

java - 在序列化之前检索 RabbitMQ 消息(审计)

转载 作者:行者123 更新时间:2023-11-30 07:53:42 29 4
gpt4 key购买 nike

我有一个对象,在将它发送到 Spring Cloud Stream 之前我试图对其进行审核,当我将对象作为 application/json 发送时出现问题,因此拦截器将对象作为序列化接收字符串。是否可以在 jackson 转换器之前运行拦截器?

拦截器代码如下:

@Slf4j
public class AuditInterceptor implements ChannelInterceptor {
private void updateField(Field field, Object payload, String newValue){
if(field.getType().equals(String.class)){
try {
Method readMethod = BeanUtils.getPropertyDescriptor(payload.getClass(),field.getName()).getReadMethod();

log.info("Old value {}", readMethod.invoke(payload));

Method setMethod = BeanUtils.getPropertyDescriptor(payload.getClass(),field.getName()).getWriteMethod();
setMethod.invoke(payload, newValue);

log.info("New value {}", readMethod.invoke(payload));
} catch (IllegalAccessException e) {
log.error("Error", e);
} catch (InvocationTargetException e) {
log.error("Error", e);
}
}
}

@Override
public Message<?> preSend(Message<?> message, MessageChannel messageChannel) {
for(Field field : message.getPayload().getClass().getDeclaredFields()){

if (field.isAnnotationPresent(CreatedBy.class)){
updateField(field, message.getPayload(), "Interceptor");
}

if (field.isAnnotationPresent(LastModifiedBy.class)){
updateField(field, message.getPayload(), "Interceptor");
}
}

return message;
}
}

运行者代码:

@Component
public class AuditRunner implements CommandLineRunner {
@Autowired
ChannelInterceptor auditInterceptor;

@Autowired
MessageChannel output;

@Override
public void run(String... strings) throws Exception {
((DirectChannel) output).addInterceptor(auditInterceptor);
}
}

最佳答案

尝试...

((DirectChannel) output).addInterceptor(0, auditInterceptor);

...因此首先应用您的拦截器(转换由拦截器执行)。

关于java - 在序列化之前检索 RabbitMQ 消息(审计),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44570493/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com