0

鉴于我有IntegrationFlowAMQP 队列的入站适配器:

@Bean
public IntegrationFlow readMessagesFlow(
        ConnectionFactory rabbitConnectionFactory,
        ObjectMapper jacksonObjectMapper) {
    return IntegrationFlows.from(
            Amqp.inboundAdapter(rabbitConnectionFactory, QUEUE)
                    .messageConverter(new Jackson2JsonMessageConverter(jacksonObjectMapper))
    )
            .log(INFO, AMQP_LOGGER_CATEGORY)
            .get();
}

当一些外部系统发送带有 JSON 正文的消息时,我发现他们使用__TypeId__=THEIR_INTERNAL_CLASS.

我想将 JSON 正文映射到我自己的类。

目前,它ClassCastException由于THEIR_INTERNAL_CLASS不可用而失败。

我怎么知道Jackson2JsonMessageConverter使用我自己的课程?

4

2 回答 2

1

这是一种方法:

Jackson2JsonMessageConverter messageConverter = new Jackson2JsonMessageConverter(jacksonObjectMapper);
DefaultClassMapper defaultClassMapper = new DefaultClassMapper();
defaultClassMapper.setDefaultType(MyOwnClass.class);
messageConverter.setClassMapper(defaultClassMapper);

messageConverter用于Amqp.inboundAdapter

于 2020-10-07T08:01:42.163 回答
1

请参阅此转换器方法:

/**
 * Set the precedence for evaluating type information in message properties.
 * When using {@code @RabbitListener} at the method level, the framework attempts
 * to determine the target type for payload conversion from the method signature.
 * If so, this type is provided in the
 * {@link MessageProperties#getInferredArgumentType() inferredArgumentType}
 * message property.
 * <p> By default, if the type is concrete (not abstract, not an interface), this will
 * be used ahead of type information provided in the {@code __TypeId__} and
 * associated headers provided by the sender.
 * <p> If you wish to force the use of the  {@code __TypeId__} and associated headers
 * (such as when the actual type is a subclass of the method argument type),
 * set the precedence to {@link Jackson2JavaTypeMapper.TypePrecedence#TYPE_ID}.
 * @param typePrecedence the precedence.
 * @see DefaultJackson2JavaTypeMapper#setTypePrecedence(Jackson2JavaTypeMapper.TypePrecedence)
 */
public void setTypePrecedence(Jackson2JavaTypeMapper.TypePrecedence typePrecedence) {

这里是关于此事的文档:https ://docs.spring.io/spring-amqp/docs/2.2.11.RELEASE/reference/html/#json-message-converter

于 2020-10-06T20:37:29.173 回答