由以下原因引起:org.springframework.jms.support.converter.MessageConversionException:在消息上找不到类型id属性[_type] [英] Caused by: org.springframework.jms.support.converter.MessageConversionException: Could not find type id property [_type] on message

查看:95
本文介绍了由以下原因引起:org.springframework.jms.support.converter.MessageConversionException:在消息上找不到类型id属性[_type]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试这个春季的JMS示例,但它给出了错误. https://spring.io/guides/gs/messaging-jms/ 引起原因:org.springframework.jms.support.converter.MessageConversionException: Could not find type id property [_type] on message from destination [queue://mailbox] 有趣的是,如果我克隆它并运行,一切运行正常.如果我复制并粘贴,则会出现错误.

I am trying this spring JMS sample, and it gives error. https://spring.io/guides/gs/messaging-jms/ Caused by: org.springframework.jms.support.converter.MessageConversionException: Could not find type id property [_type] on message from destination [queue://mailbox] Interesting part is, if I clone it and run everything runs fine. If I copy and paste, it gives error.

 @Bean // Serialize message content to json using TextMessage
public MessageConverter jacksonJmsMessageConverter() {
    MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
    converter.setTargetType(MessageType.TEXT);
    converter.setTypeIdPropertyName("_type");
    return converter;
}

这段代码实际上导致了错误.在网络和文档中搜索,我仍然不知道如何以及如何设置setTypeIdPropertyName值,以及使用"_type"设置该项目在此项目中指的是什么?由于该消息不具有此类属性,因此它来自何处?

This piece of code actually causing the error. Searching the web and documentation, I still have no clue how and what to set setTypeIdPropertyName value and with "_type" what it refers in this project to? As the message does not have such property, then where is it coming from ?

推荐答案

我使用的是Spring Boot JmsTemplate,但Danylo Zatorsky的答案的第二类对我而言并不奏效,因为其反序列化仅返回简单的字符串.在序列化过程中,在内容前加上类名,并在以后使用正则表达式对其进行破解,使人们可以反转更复杂的对象. HTH

I'm using Spring Boot JmsTemplate and the 2nd class of Danylo Zatorsky's answer didn't quite work for me as its deserialization only returns simple strings. Prepending the content with the class name during serialization and cracking that out later with a regex allows one to reverse more complex objects. HTH

@Component
public class JsonMessageConverter implements MessageConverter {

    private final ObjectMapper mapper;

    public JsonMessageConverter(ObjectMapper mapper) {
        this.mapper = mapper;
    }

    @Override
    public javax.jms.Message toMessage(Object object, Session session) throws MessageConversionException {
        try {
            // send class=<json content>
            return session.createTextMessage(object.getClass().getName() + "=" + mapper.writeValueAsString(object));
        } catch (Exception e) {
            throw new MessageConversionException("Message cannot be serialized", e);
        }
    }

    @Override
    public Object fromMessage(javax.jms.Message message) throws JMSException, MessageConversionException {
        try {
            Matcher matcher = Pattern.compile("^([^=]+)=(.+)$").matcher(((TextMessage) message).getText());
            if (!matcher.find())
            {
                throw new MessageConversionException("Message is not of the expected format: class=<json content>");
            }
            return mapper.readValue(matcher.group(2), Class.forName(matcher.group(1)));
        } catch (Exception e) {
            throw new MessageConversionException("Message cannot be deserialized", e);
        }
    }
}

这篇关于由以下原因引起:org.springframework.jms.support.converter.MessageConversionException:在消息上找不到类型id属性[_type]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆