2

私はSpring Cloud Streamで送信する前に監査しようとしているオブジェクトを持っていますが、オブジェクトをapplication/jsonとして送信すると問題が発生します。文字列。 jacksonコンバータの前にインターセプタを実行することは可能ですか?シリアル化(監査)の前に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); 
    } 
} 
+0

このインターセプタをどのようにチャンネルに適用しているかを示すことはできますか? –

+0

私の投稿をコード@GaryRussellで更新しました – hallaksec

答えて

3

ので、あなたのインターセプタが最初に印加された... ...

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

を試してみては(変換はインターセプタによって実行されます)。

関連する問題