Spring integration 告诉Jackson2JsonMessageConverter在Amqp.inboundAdapter中使用我自己的类

Spring integration 告诉Jackson2JsonMessageConverter在Amqp.inboundAdapter中使用我自己的类,spring-integration,spring-amqp,spring-integration-dsl,spring-integration-amqp,Spring Integration,Spring Amqp,Spring Integration Dsl,Spring Integration Amqp,假设我有IntegrationFlow和AMQP队列的入站适配器: @Bean public IntegrationFlow readMessagesFlow( ConnectionFactory rabbitConnectionFactory, ObjectMapper jacksonObjectMapper) { return IntegrationFlows.from( Amqp.inboundAdapter(rabbitCo

假设我有
IntegrationFlow
和AMQP队列的入站适配器:

@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正文的消息时,我发现他们使用
\uuuuuuuuuuuuuuuuuutypeid\uuuuuuuuuuu=他们的内部类

我想将JSON主体映射到我自己的类

目前,它在
ClassCastException
上失败,因为
他们的内部\u类
不可用


如何告诉Jackson2JsonMessageConverter使用我自己的类?

请参阅此转换方法:

/**
 * 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) {
/**
*设置在消息属性中计算类型信息的优先级。
*在方法级别使用{@code@RabbitListener}时,框架会尝试
*从方法签名确定有效负载转换的目标类型。
*如果是,则在中提供此类型
*{@link MessageProperties#getInferredArgumentType()inferredArgumentType}
*消息属性。
*默认情况下,如果类型是具体的(不是抽象的,不是接口),则
*在{@code\uuuuu TypeId\uuuuu}中提供的类型信息之前使用,并且
*发件人提供的关联标头。
*如果您希望强制使用{@code\uuuuuu TypeId\uuuuuu}和相关的头文件
*(例如,当实际类型是方法参数类型的子类时),
*将优先级设置为{@link Jackson2JavaTypeMapper.typepreference#TYPE_ID}。
*@param typepreference设置优先级。
*@参见DefaultJackson2JavaTypeMapper#setTypePreference(Jackson2JavaTypeMapper.TypePreference)
*/
public void setTypePreference(Jackson2JavaTypeMapper.TypePreference TypePreference){

下面是关于这个问题的文档:

请参见此转换器方法:

/**
 * 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) {
/**
*设置在消息属性中计算类型信息的优先级。
*在方法级别使用{@code@RabbitListener}时,框架会尝试
*从方法签名确定有效负载转换的目标类型。
*如果是,则在中提供此类型
*{@link MessageProperties#getInferredArgumentType()inferredArgumentType}
*消息属性。
*默认情况下,如果类型是具体的(不是抽象的,不是接口),则
*在{@code\uuuuu TypeId\uuuuu}中提供的类型信息之前使用,并且
*发件人提供的关联标头。
*如果您希望强制使用{@code\uuuuuu TypeId\uuuuuu}和相关的头文件
*(例如,当实际类型是方法参数类型的子类时),
*将优先级设置为{@link Jackson2JavaTypeMapper.typepreference#TYPE_ID}。
*@param typepreference设置优先级。
*@参见DefaultJackson2JavaTypeMapper#setTypePreference(Jackson2JavaTypeMapper.TypePreference)
*/
public void setTypePreference(Jackson2JavaTypeMapper.TypePreference TypePreference){

下面是关于这个问题的文档:

这是一种方法:

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

并在
Amqp中使用
messageConverter
。inboundAdapter

这是一种方法:

Jackson2JsonMessageConverter messageConverter = new Jackson2JsonMessageConverter(jacksonObjectMapper);
DefaultClassMapper defaultClassMapper = new DefaultClassMapper();
defaultClassMapper.setDefaultType(MyOwnClass.class);
messageConverter.setClassMapper(defaultClassMapper);
并在
Amqp.inboundAdapter