简体   繁体   English

Spring Boot JMS MarshallingMessageConverter的回复

[英]Spring Boot JMS MarshallingMessageConverter in replies

I've a JMS listener similar to the one done here: 我有一个JMS侦听器,类似于在这里完成的侦听器:

Synchronous Message send and receive using JMS template and Spring Boot 使用JMS模板和Spring Boot发送和接收同步消息

It receives a XML payload. 它接收XML有效负载。 But different from that one, I want to also return a reply. 但与那个不同的是,我还想返回一个答复。 If I return a String, it works ok, but as I'm doing with the message payload, I want to return a JAXB object. 如果我返回一个String,它可以正常工作,但是当我处理消息有效负载时,我想返回一个JAXB对象。

However, if I return a Message, Spring tries to convert the object using the SimpleMessageConverter. 但是,如果我返回一条消息,Spring会尝试使用SimpleMessageConverter转换对象。

How can I configure the MarshallingMessageConverter to be used when converting the reply payload? 转换回复有效负载时,如何配置要使用的MarshallingMessageConverter?

I had to configure the DefaultJmsListenerContainerFactory's message converter. 我必须配置DefaultJmsListenerContainerFactory的消息转换器。

But what is weird, I already have a MarshallingMessageConverter there for DefaultMessageHandlerMethodFactory and JmsMessagingTemplate, and it's from another Java package (org.springframework.messaging.converter.MarshallingMessageConverter). 但是很奇怪,我已经在其中有一个用于DefaultMessageHandlerMethodFactory和JmsMessagingTemplate的MarshallingMessageConverter,它来自另一个Java包(org.springframework.messaging.converter.MarshallingMessageConverter)。

@Bean
public JmsListenerContainerFactory<?> jmsListenerFactory(ConnectionFactory connectionFactory,
    DefaultJmsListenerContainerFactoryConfigurer configurer)
{
    DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
    factory.setErrorHandler(errorHandler());
    factory.setMessageConverter(jmsMarshallingMessageConverter());  // !!!!

    configurer.configure(factory, connectionFactory);

    return factory;
}

@Bean
public org.springframework.jms.support.converter.MarshallingMessageConverter jmsMarshallingMessageConverter()
{
    Jaxb2Marshaller marshaller = marshaller();

    org.springframework.jms.support.converter.MarshallingMessageConverter converter =
        new org.springframework.jms.support.converter.MarshallingMessageConverter();
    converter.setMarshaller(marshaller);
    converter.setUnmarshaller(marshaller);
    converter.setTargetType(MessageType.TEXT);

    return converter;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM