2
我有我想送它春雲流之前審計的對象,問題就出現了,當我發送的對象爲application/json
所以攔截器接收對象的序列化串。傑克遜轉換器之前可以運行攔截器嗎?檢索之前的序列化(審計)一RabbitMQ的消息
下面是攔截器的代碼:
@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);
}
}
你能告訴你如何運用這個攔截器的通道? –
我更新後的代碼@GaryRussell – hallaksec