我有Spring Boot应用程序
它正在从ActiveMQ接收Json格式的消息
@Component class Receiver {
@JmsListener(destination = "queue")
public void receiveMessage(BusMessage message) {
System.out.println("Received <" + message + ">");
}
问题在于,有时Json会在没有正确头的情况下到达
在这种情况下,我会例外
org.springframework.messaging.converter.MessageConversionException: Cannot convert from [java.lang.String] to [cam.melexis.minipcs2socketioserver.BusMessage] for org.springframework.jms.listener.adapter.AbstractAdaptableMessageListener$MessagingMessageConverterAdapter$LazyResolutionMessage@38bd4ff9, failedMessage=org.springframework.jms.listener.adapter.AbstractAdaptableMessageListener$MessagingMessageConverterAdapter$LazyResolutionMessage@38bd4ff9
一种解决方案是将消息作为字符串获取并反序列化
@Component class Receiver {
@JmsListener(destination = "queue")
public void receiveScannerMessage(Message message, Session session) {
System.out.println("Received <" + message + ">");
}
但是我想做些更优雅的事情
要简化所有@JmsListener
并使我在Spring的学习曲线更加弯曲:)
知道什么,所有消息将以Json格式到达,我可以在所有传入消息中添加缺少的标头吗?
还是存在另一种“更好”的方式?
答案 0 :(得分:0)
This made a job
@EnableJms
@Configuration class JmsListenerConfig implements JmsListenerConfigurer {
Logger logger = LoggerFactory.getLogger(Receiver.class);
@Bean
public DefaultMessageHandlerMethodFactory handlerMethodFactory() {
DefaultMessageHandlerMethodFactory factory = new DefaultMessageHandlerMethodFactory();
factory.setMessageConverter(messageConverter());
return factory;
}
@Bean
public MessageConverter messageConverter() {
return new MappingJackson2MessageConverter();
}
@Override
public void configureJmsListeners(JmsListenerEndpointRegistrar registrar) {
registrar.setMessageHandlerMethodFactory(handlerMethodFactory());
}
}
But i have no idea how
Maybe anybody can help me with this?