Spring @JmsListener 无法将 json 转换为对象

Spring @JmsListener is not able to convert json into object

我原以为@JmsListener 会自动将 json 对象转换为我的对象,但它的有效负载将数据作为 json 格式的字符串而不是实际对象返回。

    @JmsListener(destination = "${default-queue-name-to-listen}")
    public void receiveMessage(final Message<MyObject> message) throws JMSException {
        logger.info("message received from the queue/topic : {}", message);
        MyObject response = message.getPayload();
}

但是我们收到的响应是这样的字符串格式:{"id":"1","name":"2222"}

这在运行时失败了。

我的另一段代码是:

@Bean
public JmsListenerContainerFactory jmsListenerContainerFactory(ConnectionFactory connectionFactory) throws URLSyntaxException {
    DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
    factory.setConnectionFactory(connectionFactory);
    factory.setSubscriptionDurable(true);
    factory.setPubSubDomain(true);
    return factory;
}

这取决于制作者发送的内容。

如果发送了短信,您会收到一个字符串。如果发送了一个对象消息,您可以获得一个对象。