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

RabbitListener returning String as ASCII instead of String

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

Rabbit MQ Message

Exchange raw-cdr

Routing Key uk

Redelivered
priority: 0

delivery_mode: 2

headers:

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

content_encoding: UTF-8

content_type: text/plain

圣思网络公司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 启动默认值

@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();
    } 

尽管在警告中它声明它不能转换

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

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

如果您有一些消息是 text/plain 和一些消息是 application/json,您应该配置 ContentTypeDelegatingMessageConverterSimpleMessageConverter 以及 JSON转换器。