json 到对象转换期间出现异常:无法解析“javaTypes”中的“json__TypeId__”

问题描述 投票:0回答:1

我正在读取带有 json 消息的队列,并且在侦听器中我尝试将消息作为该 json 中的对象进行访问。这是使用的转换器:

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

我面临以下异常。

org.springframework.integration.transformer.MessageTransformationException: failed to transform message; nested exception is java.lang.IllegalArgumentException: Could not resolve 'json__TypeId__' in 'javaTypes'.

以下是示例 json,我使用 jmsTemplate 将其添加到队列中。

{ “id”:1, "tradeSourceId": "mytradeSourceId", "messageOriginCode": "CXE", "序列号": "1", "messageType" : "传输", “运动代码”:“1”, “传输类型”:“正常”, "updateType" : "无更新" }

请帮忙。

json jms spring-jms
1个回答
1
投票
>converter.setTypeIdPropertyName("BrokerTrade.class");

这应该是消息中 String 属性的名称,其中包含有关要转换到的类型的信息 - 要么是要在类映射中查找以确定类名称的值的完全限定类名称。

如果消息不包含类型信息,则必须对转换器进行子类化并重写

getJavaTypeForMessage
以返回 Jackson
JavaType
...

/**
 * Determine a Jackson JavaType for the given JMS Message,
 * typically parsing a type id message property.
 * <p>The default implementation parses the configured type id property name
 * and consults the configured type id mapping. This can be overridden with
 * a different strategy, e.g. doing some heuristics based on message origin.
 * @param message the JMS Message to set the type id on
 * @throws JMSException if thrown by JMS methods
 * @see #setTypeIdOnMessage(Object, javax.jms.Message)
 * @see #setTypeIdPropertyName(String)
 * @see #setTypeIdMappings(java.util.Map)
 */
protected JavaType getJavaTypeForMessage(Message message) throws JMSException {
...
}

您通常可以使用 objectMapper 来做到这一点...

objectMapper.getTypeFactory().constructType(BrokerTrade.class)
© www.soinside.com 2019 - 2024. All rights reserved.