Jackson2JsonMessageConverter 不适用于rabbitmq [英] Jackson2JsonMessageConverter not work for rabbitmq

查看:77
本文介绍了Jackson2JsonMessageConverter 不适用于rabbitmq的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码进行消息转换器:

I use following code for message converter:

SimpleMessageListenerContainer container(ConnectionFactory  connectionFactory, Queue queue,
        MessageListenerAdapter listenerAdapter) {

    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
    container.setConnectionFactory(connectionFactory);
    container.setQueueNames(queue.getName());
    container.setMessageListener(listenerAdapter);
    container.setMessageConverter(new Jackson2JsonMessageConverter());
    return container;
}

我的监听器已声明:

public void receiveMessage(List<Map<String, Object>> message) {
    try {
        System.out.println("Received <" + new String(message, "UTF-8") +     ">");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
}

但它总是试图给出以下错误:

But it always try to gives follow error:

Failed to invoke target method 'receiveMessage' with argument type = [class [B], value = [{[B@40c2d9c5}]","at org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter.invokeListenerMethod(MessageListenerAdapter.java:408)

它似乎试图调用 byte[] 作为参数而不是将 json 字符串转换为 List>.

It seems it tries to invoke byte[] as argument instead of convert json string to List>.

推荐答案

转换器需要包含标记 jsoncontent_type 消息属性 - 例如应用程序/json.如果您使用的至少是 1.6.1 版本,您应该会看到一条 WARN 日志.

The converter requires a content_type message property that contains the token json - e.g. application/json. You should see a WARN log if you are using at least version 1.6.1.

log.warn("Could not convert incoming message with content-type ["
        + contentType + "], 'json' keyword missing.");

如果您无法更改生产者以正确设置内容类型,您可以将转换器子类化...

If you can't change the producer to set the content type properly, you can subclass the converter...

@Override
public Object fromMessage(Message message, Object conversionHint) throws MessageConversionException {
    message.getMessageProperties().setContentType("application/json");
    return super.fromMessage(message, conversionHint);
}

这篇关于Jackson2JsonMessageConverter 不适用于rabbitmq的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆