我刚刚将我的应用程序更新为版本spring-integration-amqp-5.2.4.RELEASE
,当我从队列接收消息时,一切正常,但是我注意到java.lang.ClassNotFoundException
类中的警告DefaultAmqpHeaderMapper
。我将自定义DefaultJackson2JavaTypeMapper
与IdClassMapping
结合使用。如何避免跟踪警告?
@Bean
public Jackson2JavaTypeMapper defaultJackson2JavaTypeMapper() {
DefaultJackson2JavaTypeMapper defaultJackson2JavaTypeMapper = new DefaultJackson2JavaTypeMapper();
Map<String, Class<?>> classMapping = new HashMap<>();
classMapping.put("event.request", RequestDTO.class);
defaultJackson2JavaTypeMapper.setIdClassMapping(classMapping);
return defaultJackson2JavaTypeMapper;
}
@Bean
public MessageConverter messageConverterJson(ObjectMapper objectMapper) {
Jackson2JsonMessageConverter messageConverter = new Jackson2JsonMessageConverter(objectMapper);
messageConverter.setJavaTypeMapper(defaultJackson2JavaTypeMapper());
return messageConverter;
}
日志警告
o.s.i.a.support.DefaultAmqpHeaderMapper : error occurred while mapping from AMQP properties to MessageHeaders
java.lang.IllegalStateException: java.lang.ClassNotFoundException: event.request
at org.springframework.integration.mapping.support.JsonHeaders.getClassForValue(JsonHeaders.java:89)
at org.springframework.integration.mapping.support.JsonHeaders.buildResolvableType(JsonHeaders.java:72)
at org.springframework.integration.amqp.support.DefaultAmqpHeaderMapper.createJsonResolvableTypHeaderInAny(DefaultAmqpHeaderMapper.java:169)
at org.springframework.integration.amqp.support.DefaultAmqpHeaderMapper.extractStandardHeaders(DefaultAmqpHeaderMapper.java:155)
at org.springframework.integration.amqp.support.DefaultAmqpHeaderMapper.extractStandardHeaders(DefaultAmqpHeaderMapper.java:61)
at org.springframework.integration.mapping.AbstractHeaderMapper.toHeaders(AbstractHeaderMapper.java:266)
at org.springframework.integration.mapping.AbstractHeaderMapper.toHeadersFromRequest(AbstractHeaderMapper.java:203)
at org.springframework.integration.amqp.support.DefaultAmqpHeaderMapper.toHeadersFromRequest(DefaultAmqpHeaderMapper.java:337)
at org.springframework.integration.amqp.support.DefaultAmqpHeaderMapper.toHeadersFromRequest(DefaultAmqpHeaderMapper.java:61)
at org.springframework.integration.amqp.inbound.AmqpInboundGateway$Listener.convert(AmqpInboundGateway.java:351)
at org.springframework.integration.amqp.inbound.AmqpInboundGateway$Listener.onMessage(AmqpInboundGateway.java:314)
at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.doInvokeListener(AbstractMessageListenerContainer.java:1579)
at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.actualInvokeListener(AbstractMessageListenerContainer.java:1498)
at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.invokeListener(AbstractMessageListenerContainer.java:1486)
at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.doExecuteListener(AbstractMessageListenerContainer.java:1477)
at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.executeListener(AbstractMessageListenerContainer.java:1421)
at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.doReceiveAndExecute(SimpleMessageListenerContainer.java:963)
at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.receiveAndExecute(SimpleMessageListenerContainer.java:913)
at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.access$1600(SimpleMessageListenerContainer.java:81)
at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.mainLoop(SimpleMessageListenerContainer.java:1284)
at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.run(SimpleMessageListenerContainer.java:1190)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.ClassNotFoundException: event.request
at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at org.springframework.boot.loader.LaunchedURLClassLoader.loadClass(LaunchedURLClassLoader.java:92)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:348)
at org.springframework.util.ClassUtils.forName(ClassUtils.java:282)
at org.springframework.integration.mapping.support.JsonHeaders.getClassForValue(JsonHeaders.java:86)
... 21 common frames omitted
由于您已经通过Jackson2JsonMessageConverter
将AMQP消息映射到目标有效负载,因此看起来根本不需要任何传入标头。
为了避免该警告和额外的映射工作,建议您注入自定义AmqpHeaderMapper
。当您从请求中返回用于映射的空标头时,可能只是DefaultAmqpHeaderMapper
的扩展:
amqpInboundGateway.setHeaderMapper(new DefaultAmqpHeaderMapper(null, null) {
@Override
public Map<String, Object> toHeadersFromRequest(MessageProperties source) {
return new HashMap<>();
}
});
另一方面,这只是警告:您可以简单地将org.springframework.integration.amqp.inbound
类别的日志记录级别降低为ERROR。
关键是,如果找不到__Type__
标头的类,就不能忽略这样的错误。该框架无法做出您所期望的假设。
另一种可能的解决方法是,当您覆盖MessageProperties.getHeaders()
时,从Jackson2JsonMessageConverter
扩展名的fromMessage()
中删除那些JSON标头。