MessageConversionException:无法转换邮件内容

时间:2016-10-25 13:03:36

标签: rabbitmq spring-integration spring-amqp spring-rabbitmq

我有一个使用Spring AMQP的SpringBoot应用程序。目前,我正在使用JavaConfig类实现接收器端,正如Spring官方官方示例所指示的那样。但是,我收到一个MessageConversionException,并且消息甚至没有到达我的ClientHandler类来处理消息。以下是事件的控制台日志:

Caused by: org.springframework.amqp.support.converter.MessageConversionException: Failed to      convert Message content
    at org.springframework.amqp.support.converter.JsonMessageConverter.fromMessage(JsonMessageConverter.java:105) ~[spring-amqp-1.4.6.RELEASE.jar:na]
    at org.springframework.amqp.rabbit.listener.adapter.AbstractAdaptableMessageListener.extractMessage(AbstractAdaptableMessageListener.java:185) ~[spring-rabbit-1.4.6.RELEASE.jar:na]
    at org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter.onMessage(MessageListenerAdapter.java:243) ~[spring-rabbit-1.4.6.RELEASE.jar:na]
    at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.doInvokeListener(AbstractMessageListenerContainer.java:756) ~[spring-rabbit-1.4.6.RELEASE.jar:na]
    ... 10 common frames omitted
Caused by: org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of byte out of START_OBJECT token
 at [Source: java.io.StringReader@33b39883; line: 1, column: 1]
    at org.codehaus.jackson.map.JsonMappingException.from(JsonMappingException.java:163) ~[jackson-mapper-asl-1.9.13.jar:1.9.13]
    at org.codehaus.jackson.map.deser.StdDeserializationContext.mappingException(StdDeserializationContext.java:219) ~[jackson-mapper-asl-1.9.13.jar:1.9.13]
    at org.codehaus.jackson.map.deser.std.StdDeserializer._parseByte(StdDeserializer.java:214) ~[jackson-mapper-asl-1.9.13.jar:1.9.13]
    at org.codehaus.jackson.map.deser.std.StdDeserializer$ByteDeserializer.deserialize(StdDeserializer.java:749) ~[jackson-mapper-asl-1.9.13.jar:1.9.13]
    at org.codehaus.jackson.map.deser.std.StdDeserializer$ByteDeserializer.deserialize(StdDeserializer.java:736) ~[jackson-mapper-asl-1.9.13.jar:1.9.13]
    at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:2732) ~[jackson-mapper-asl-1.9.13.jar:1.9.13]
    at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1877) ~[jackson-mapper-asl-1.9.13.jar:1.9.13]
    at org.springframework.amqp.support.converter.JsonMessageConverter.convertBytesToObject(JsonMessageConverter.java:131) ~[spring-amqp-1.4.6.RELEASE.jar:na]
    at org.springframework.amqp.support.converter.JsonMessageConverter.fromMessage(JsonMessageConverter.java:100) ~[spring-amqp-1.4.6.RELEASE.jar:na]
    ... 13 common frames omitted
2016-10-25 12:51:42,020 WARN  t:[SimpleAsyncTaskExecutor-1] ConditionalRejectingErrorHandler: Fatal message conversion error; message rejected; it will be dropped or routed to a dead letter exchange, if so configured: (Body:'{"messageDTOList":[{"createdDate":1477396301061,"message":"{\"mac\":-99,\"device\":444,\"ifindex\":55,\"ip\":668,\"arpdevice\":99,\"aprifindex\":\"\",\"vlanindex\":111,\"enddevice\":133}","crudOperation":"UPDATE"}],"senderServerId":"5b36a7a1-c17d-471f-b16e-e8e0bf4afb5b","messageId":"5b36a7a1-c17d-471f-b16e-e8e0bf4afb5b20","rebasoftServerType":"AC","taskType":"MAC"}'MessageProperties [headers={}, timestamp=null, messageId=null, userId=null, appId=null, clusterId=null, type=null, correlationId=[53, 98, 51, 54, 97, 55, 97, 49, 45, 99, 49, 55, 100, 45, 52, 55, 49, 102, 45, 98, 49, 54, 101, 45, 101, 56, 101, 48, 98, 102, 52, 97, 102, 98, 53, 98, 50, 48], replyTo=null, contentType=application/json, contentEncoding=null, contentLength=0, deliveryMode=PERSISTENT, expiration=null, priority=0, redelivered=false, receivedExchange=X, receivedRoutingKey=data.core.macs, deliveryTag=2, messageCount=0])
org.springframework.amqp.rabbit.listener.exception.ListenerExecutionFailedException: Listener threw exception
    at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.wrapToListenerExecutionFailedExceptionIfNeeded(AbstractMessageListenerContainer.java:864) ~[spring-rabbit-1.4.6.RELEASE.jar:na]
    at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.doInvokeListener(AbstractMessageListenerContainer.java:759) ~[spring-rabbit-1.4.6.RELEASE.jar:na]
    at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.invokeListener(AbstractMessageListenerContainer.java:679) ~[spring-rabbit-1.4.6.RELEASE.jar:na]
    at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.access$001(SimpleMessageListenerContainer.java:83) [spring-rabbit-1.4.6.RELEASE.jar:na]
    at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$1.invokeListener(SimpleMessageListenerContainer.java:170) ~[spring-rabbit-1.4.6.RELEASE.jar:na]
    at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.invokeListener(SimpleMessageListenerContainer.java:1253) [spring-rabbit-1.4.6.RELEASE.jar:na]
    at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.executeListener(AbstractMessageListenerContainer.java:660) ~[spring-rabbit-1.4.6.RELEASE.jar:na]
    at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.doReceiveAndExecute(SimpleMessageListenerContainer.java:1021) [spring-rabbit-1.4.6.RELEASE.jar:na]
    at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.receiveAndExecute(SimpleMessageListenerContainer.java:1005) [spring-rabbit-1.4.6.RELEASE.jar:na]
    at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.access$700(SimpleMessageListenerContainer.java:83) [spring-rabbit-1.4.6.RELEASE.jar:na]
    at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.run(SimpleMessageListenerContainer.java:1115) [spring-rabbit-1.4.6.RELEASE.jar:na]
    at java.lang.Thread.run(Thread.java:745) [na:1.8.0_51]
Caused by: org.springframework.amqp.support.converter.MessageConversionException: Failed to convert Message content
    at org.springframework.amqp.support.converter.JsonMessageConverter.fromMessage(JsonMessageConverter.java:105) ~[spring-amqp-1.4.6.RELEASE.jar:na]
    at org.springframework.amqp.rabbit.listener.adapter.AbstractAdaptableMessageListener.extractMessage(AbstractAdaptableMessageListener.java:185) ~[spring-rabbit-1.4.6.RELEASE.jar:na]
    at org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter.onMessage(MessageListenerAdapter.java:243) ~[spring-rabbit-1.4.6.RELEASE.jar:na]
    at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.doInvokeListener(AbstractMessageListenerContainer.java:756) ~[spring-rabbit-1.4.6.RELEASE.jar:na]
    ... 10 common frames omitted
Caused by: org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of byte out of START_OBJECT token
 at [Source: java.io.StringReader@33b39883; line: 1, column: 1]
    at org.codehaus.jackson.map.JsonMappingException.from(JsonMappingException.java:163) ~[jackson-mapper-asl-1.9.13.jar:1.9.13]
    at org.codehaus.jackson.map.deser.StdDeserializationContext.mappingException(StdDeserializationContext.java:219) ~[jackson-mapper-asl-1.9.13.jar:1.9.13]
    at org.codehaus.jackson.map.deser.std.StdDeserializer._parseByte(StdDeserializer.java:214) ~[jackson-mapper-asl-1.9.13.jar:1.9.13]
    at org.codehaus.jackson.map.deser.std.StdDeserializer$ByteDeserializer.deserialize(StdDeserializer.java:749) ~[jackson-mapper-asl-1.9.13.jar:1.9.13]
    at org.codehaus.jackson.map.deser.std.StdDeserializer$ByteDeserializer.deserialize(StdDeserializer.java:736) ~[jackson-mapper-asl-1.9.13.jar:1.9.13]
    at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:2732) ~[jackson-mapper-asl-1.9.13.jar:1.9.13]
    at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1877) ~[jackson-mapper-asl-1.9.13.jar:1.9.13]
    at org.springframework.amqp.support.converter.JsonMessageConverter.convertBytesToObject(JsonMessageConverter.java:131) ~[spring-amqp-1.4.6.RELEASE.jar:na]
    at org.springframework.amqp.support.converter.JsonMessageConverter.fromMessage(JsonMessageConverter.java:100) ~[spring-amqp-1.4.6.RELEASE.jar:na]
    ... 13 common frames omitted

我的客户课程如下:

AbstractEMCRabbitConfiguration.java

@Configuration
public abstract class AbstractEMCRabbitConfiguration {

//@Value("${emc.rabbit.exchange.core}")
protected static String CORE_DATA_EXCHANGE_NAME = "X";

@Value("${emc.rabbit.queue.core.macs}")
protected static String MAC_REQUEST_QUEUE_NAME = "data.core.macs";

protected static String MAC_REQUEST_ROUTING_KEY = MAC_REQUEST_QUEUE_NAME;

//@Value("${emc.rabbit.hostname}")
protected String hostname;

//@Value("${emc.rabbit.username}")
protected String username;

//@Value("${emc.rabbit.password}")
protected String password;

//@Value("${emc.rabbit.port:5672}")
protected Integer port = 5672;

protected abstract void configureRabbitTemplate(RabbitTemplate template);

@Bean
public ConnectionFactory connectionFactory(){
    CachingConnectionFactory connectionFactory = new CachingConnectionFactory("localhost");
    connectionFactory.setUsername("test");
    connectionFactory.setPassword("test");
    connectionFactory.setPort(port);
    connectionFactory.setRequestedHeartBeat(60);
    return connectionFactory;
}

@Bean
public RabbitTemplate rabbitTemplate(){
    RabbitTemplate template = new RabbitTemplate(connectionFactory());
    template.setMessageConverter(jsonMessageConverter());
    configureRabbitTemplate(template);
    return template;
}

@Bean
public MessageConverter jsonMessageConverter() {
    JsonMessageConverter jsonMessageConverter = new JsonMessageConverter();
    jsonMessageConverter.setClassMapper(defaultClassMapper());
    return jsonMessageConverter;
}

@Bean
public DefaultClassMapper defaultClassMapper() {
    DefaultClassMapper defaultClassMapper = new DefaultClassMapper();
    defaultClassMapper.setDefaultType(byte.class);
    return defaultClassMapper;
}

@Bean
public DirectExchange coreDataExchange() { return new DirectExchange(CORE_DATA_EXCHANGE_NAME); }

@Bean
public AmqpAdmin amqpAdmin() {
    RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory());
    return rabbitAdmin;
}
}

RabbitClientConfiguration.java

@Configuration
@Import({JacksonConfiguration.class, MessagingConfiguration.class})
public class RabbitClientConfiguration extends AbstractEMCRabbitConfiguration {

@Inject
private JacksonConfiguration jacksonConfiguration;

@Inject
private MessagingConfiguration messagingConfiguration;

//@Value("${data.core.pattern}")
//private String coreDataRoutingKey;

//@Inject
//private ClientHandler clientHandler;

@Override
public void configureRabbitTemplate(RabbitTemplate rabbitTemplate) {
    rabbitTemplate.setRoutingKey("data.core.macs");
}

@Bean
public SimpleMessageListenerContainer messageListenerContainer(){
    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
    container.setConnectionFactory(connectionFactory());
    container.setQueues(macsDataQueue());
    container.setMessageListener(messageListenerAdapter());
    container.setAcknowledgeMode(AcknowledgeMode.AUTO);
    return container;
}

@Bean
MessageListenerAdapter messageListenerAdapter(){
    return new MessageListenerAdapter(clientHandler(jacksonConfiguration.objectMapper(),messagingConfiguration.macMessageHandler(messagingConfiguration.messagingFactory())),jsonMessageConverter());
    //return new MessageListenerAdapter(clientHandler,"receiveMessage");
}

@Bean
ClientHandler clientHandler(ObjectMapper objectMapper, IMessageHandler macMessageHandler){
    ClientHandler clientHandler = new ClientHandler();
    clientHandler.setObjectMapper(objectMapper);
    clientHandler.setMacMessageHandler(macMessageHandler);
    return clientHandler;
}

@Bean
public Queue macsDataQueue(){
    return rabbitAdmin().declareQueue();
}

@Bean
public Binding macsDataBinding(){
    return BindingBuilder.bind(macsDataQueue()).to(coreDataExchange()).with("data.core.macs");
}

@Bean
public AmqpAdmin rabbitAdmin() { return new RabbitAdmin(connectionFactory());}

}

从代理收到的典型消息如下所示:

  

(身体:' {" messageDTOList":[{" createdDate":1477396301061,"消息":" {\&# 34; MAC \": - 99,\"设备\":444,\"索引序号\":55,\" IP \&#34 ;:668,\" arpdevice \":99,\" aprifindex \":\" \" \" vlanindex \&# 34;:111,\" enddevice \" 133}"" crudOperation":" UPDATE"}]," senderServerId&# 34;:" 5b36a7a1-c17d-471f-B16E-e8e0bf4afb5b"" MESSAGEID":" 5b36a7a1-c17d-471f-B16E-e8e0bf4afb5b20"" rebasoftServerType":" AC"," taskType":" MAC"}' MessageProperties [headers = {},timestamp = null,messageId = null ,userId = null,appId = null,clusterId = null,type = null,correlationId = [53,58,51,54,97,55,97,49,45,99,49,55,100,45,52, 55,49,102,45,98,59,54,101,45,101,56,101,48,98,102,52,97,102,98,53,98,50,48],replyTo = null ,contentType = application / json,contentEncoding = null,contentLength = 0,deliveryMode = PERSISTENT,expiration = null,priority = 0,red elivered = false,receivedExchange = X,receivedRoutingKey = data.core.macs,deliveryTag = 2,messageCount = 0])

注意:内容类型为' application / json'

任何帮助都会让我非常感激。

4 个答案:

答案 0 :(得分:1)

我发现了问题,我的配置类中的MessageListenerAdapter似乎不喜欢带有构造函数参数的ClientHandler。一旦我删除了构造函数参数,它就可以正常工作。

答案 1 :(得分:0)

  

引起:org.codehaus.jackson.map.JsonMappingException:无法反序列化START_OBJECT标记中的字节实例

看起来像垃圾邮件 - 你确定它是由带有json转换器的兔子模板创建的吗?

您可以编辑问题并发布消息内容(以及content_type属性和其他标题)吗? (您可以使用管理界面来检查消息)。

编辑

使用此应用程序解码您的邮件时没有问题...

@SpringBootApplication
public class So40240771Application {

    public static void main(String[] args) throws Exception {
        ConfigurableApplicationContext context = SpringApplication.run(So40240771Application.class, args);
        RabbitTemplate template = context.getBean(RabbitTemplate.class);
        String body = "{"
                + "\"messageDTOList\":"
                + "[{\"createdDate\":1477396301061,"
                +   "\"message\":\"{\\\"mac\\\":-99,\\\"device\\\":444,\\\"ifindex\\\":55,\\\"ip\\\":668,\\\"arpdevice\\\":99,\\\"aprifindex\\\":\\\"\\\",\\\"vlanindex\\\":111,\\\"enddevice\\\":133}\","
                +   "\"crudOperation\":\"UPDATE\"}],"
                + "\"senderServerId\":\"5b36a7a1-c17d-471f-b16e-e8e0bf4afb5b\","
                + "\"messageId\":\"5b36a7a1-c17d-471f-b16e-e8e0bf4afb5b20\","
                + "\"rebasoftServerType\":\"AC\","
                + "\"taskType\":\"MAC\""
                + "}";
        MessageProperties properties = new MessageProperties();
        properties.setContentType("application/json");
        template.send("foo", MessageBuilder.withBody(body.getBytes()).andProperties(properties).build());
        Thread.sleep(6000);
        context.close();
    }

    @Bean
    public Jackson2JsonMessageConverter converter() {
        Jackson2JsonMessageConverter converter = new Jackson2JsonMessageConverter();
        return converter;
    }

    @Bean
    public SimpleMessageListenerContainer container(ConnectionFactory connectionFactory) {
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory);
        container.setMessageListener(new MessageListenerAdapter(new Object() {
            void handleMessage(Object in) {
                System.out.println(in.getClass() + "\n" + in);
            }
        }, converter()));
        container.setQueueNames("foo");
        return container;
    }

}

结果:

class java.util.LinkedHashMap
{messageDTOList=[{createdDate=1477396301061, message={"mac":-99,"device":444,"ifindex":55,"ip":668,"arpdevice":99,"aprifindex":"","vlanindex":111,"enddevice":133}, crudOperation=UPDATE}], senderServerId=5b36a7a1-c17d-471f-b16e-e8e0bf4afb5b, messageId=5b36a7a1-c17d-471f-b16e-e8e0bf4afb5b20, rebasoftServerType=AC, taskType=MAC}

需要注意的一些事项:由于消息在标头中没有类型信息(如果使用了出站转换器,它会添加),消息将被解码为简单的Map

如果要解码为对象,则需要标题中的类型信息,或者需要使用类映射器(例如DefaultClassMapper)配置转换器。

其次,它看起来像"消息" element是双重编码的JSON - 它已经是JSON并且已被重新编码)。

答案 2 :(得分:0)

您的有效负载/数据类中不需要参数构造函数。

答案 3 :(得分:0)

更新模型类以实现 Serializable 接口为我解决了这个问题。