RabbitListener将String作为ASCII而不是String返回

时间:2016-12-15 15:26:19

标签: spring-integration spring-amqp

由于某种原因,RabbitListener显示的是ASCII表示而不是实际的字符串

  

Rabbit MQ消息

     

交换raw-cdr

     

路由密钥英国

     

的Redelivered
  优先级:0

     

delivery_mode:2

     

标题:

     
    

file_name:/Users/shahbour/IdeaProjects/micro-services/file-sftp-service/ftp-inbound/LONSBC01.20161214142000.10120CB.ACT

         

content_encoding:UTF-8

         

content_type:text / plain

  
     

Sonus Networks,Inc .00000000FF600000520000000000000128V04.02.05R000   0000000000000000000000000000ACT2016121414100000000000000000

但我得到的是

  

83,111,110,117,115,32,78,101,116,119,111,114,107,115,44,32,73,110,99,46,48,48,48,48,48,48,48,48,70,70,54,48,48,48,48,48 ,53,50,48,48,48,48,48,48,48,48,48,48,48,48,48,49,50,56,86,48,52,46,48,50,46 ,48,53,82,48,48,48,32,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48 ,48,48,48,48,48,48,48,48,48,48,65,67,84,50,48,49,54,49,50,49,52,49,52,49,48 ,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48

以下是我正在使用的代码,每件事都是spring boot default

@RabbitListener(queues = {"fr-cdr.panel","uk-cdr.panel"})
public void messageHandler(Message<String> rawCdr, @Header String file_name) throws InterruptedException {
    log.info(rawCdr);

}

制作人来自另一个节目,如下所示

    @Bean
    @ServiceActivator(inputChannel = "linesChannel")
    public AmqpOutboundEndpoint amqpOutbound(AmqpTemplate amqpTemplate) {
        AmqpOutboundEndpoint outbound = new AmqpOutboundEndpoint(amqpTemplate);
        outbound.setExchangeName(exchangeName);
        outbound.setRoutingKey(routingKey);
        return outbound;
    }

启用调试模式后,问题显示

2016-12-16 07:46:03.714 DEBUG 81519 --- [cTaskExecutor-1] o.s.a.r.listener.BlockingQueueConsumer   : Received message: (Body:'Sonus Networks, Inc.00000000FF600000520000000000000128V04.02.05R000 0000000000000000000000000000ACT2016121414100000000000000000' MessageProperties [headers={file_name=/Users/shahbour/IdeaProjects/micro-services/file-sftp-service/ftp-inbound/LONSBC01.20161214142000.10120CB.ACT}, timestamp=null, messageId=null, userId=null, receivedUserId=null, appId=null, clusterId=null, type=null, correlationId=null, correlationIdString=null, replyTo=null, contentType=text/plain, contentEncoding=UTF-8, contentLength=0, deliveryMode=null, receivedDeliveryMode=PERSISTENT, expiration=null, priority=0, redelivered=true, receivedExchange=raw-cdr, receivedRoutingKey=uk, receivedDelay=null, deliveryTag=1, messageCount=0, consumerTag=amq.ctag-15IUBH6oN_hheTvluP4YfQ, consumerQueue=uk-cdr.panel])
2016-12-16 07:46:03.717  WARN 81519 --- [cTaskExecutor-1] o.s.a.s.c.Jackson2JsonMessageConverter   : Could not convert incoming message with content-type [text/plain]
2016-12-16 07:46:03.724 DEBUG 81519 --- [cTaskExecutor-1] .a.r.l.a.MessagingMessageListenerAdapter : Processing [GenericMessage [payload=byte[127], headers={amqp_receivedDeliveryMode=PERSISTENT, amqp_receivedRoutingKey=uk, amqp_contentEncoding=UTF-8, amqp_receivedExchange=raw-cdr, amqp_deliveryTag=1, file_name=/Users/shahbour/IdeaProjects/micro-services/file-sftp-service/ftp-inbound/LONSBC01.20161214142000.10120CB.ACT, amqp_consumerQueue=uk-cdr.panel, amqp_redelivered=true, id=c645580a-6f8d-6649-1426-14d06fbc1ddf, amqp_consumerTag=amq.ctag-15IUBH6oN_hheTvluP4YfQ, contentType=text/plain, timestamp=1481867163721}]]

在评论下面的Jackson2JsonMessageConverter之后,每件事情都做得很好。

    @Bean
    public MessageConverter jackson2json() {
        return new Jackson2JsonMessageConverter();
    } 

即使在警告中它声明它无法转换

1 个答案:

答案 0 :(得分:1)

如果在应用程序上下文中声明了一个类型为MessageConverter的bean,则Spring Boot将自动配置侦听器容器适配器的消息转换器。

转换器需要的内容类型为application/json

如果您有一些text/plain和一些application/json的消息,则应配置ContentTypeDelegatingMessageConverter SimpleMessageConverter和JSON转换器。