如何在Spring-AMQP中使用Jackson2JsonMessageConverter处理内容类型null

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

如果it's not one problemit's another ...我似乎已经解决了之前的2个问题,但是现在当在测试环境之外运行时,有一个站起来的应用程序我看到了这个。

o.s.a.s.c.Jackson2JsonMessageConverter   : Could not convert incoming message with content-type [null], 'json' keyword missing.

像以前一样,这个消息似乎很清楚。哎呀,事情甚至在独立的测试环境中工作,但是当在独立服务器上运行时,代码似乎采取了不同的路径并且由于相同的原因而失败,但是通过不同的组件。

@Configuration
open class MessagingConfig {

    @Bean
    open fun jackson2Json(om: ObjectMapper): SmartMessageConverter {
        return Jackson2JsonMessageConverter(om)
    }

    @Bean
    open fun mappingJackson2(om: ObjectMapper): MappingJackson2MessageConverter {
        val mc = MappingJackson2MessageConverter()
        mc.objectMapper = om
        return mc
    }

    @Bean
    open fun defaultMessageHandlerMethodFactory(jackson: MappingJackson2MessageConverter): DefaultMessageHandlerMethodFactory {
        val factory = DefaultMessageHandlerMethodFactory()
        factory.setMessageConverter(jackson)
        return factory
    }


    @Bean
    open fun builder(): Jackson2ObjectMapperBuilderCustomizer {
        return Jackson2ObjectMapperBuilderCustomizer {
            it.modules(JavaTimeModule(), KotlinModule())
            it.featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
        }
    }


    @Configuration
    open class RabbitConfigurer(val dmhmf: DefaultMessageHandlerMethodFactory) : RabbitListenerConfigurer {
        override fun configureRabbitListeners(registrar: RabbitListenerEndpointRegistrar?) {
            registrar?.messageHandlerMethodFactory = dmhmf
        }
    }
}
java spring kotlin spring-amqp
2个回答
2
投票

您可以将MessagePostprocessor添加到容器工厂,以使用content_id属性增强消息。

factory.setAfterReceivePostProcessors(m -> {
    m.getMessageProperties().setContentId("application/json");
    return m;
}

0
投票

根据AbstractJackson2MessageConverter的逻辑,contentType AMQP财产是必需的:

MessageProperties properties = message.getMessageProperties();
    if (properties != null) {
        String contentType = properties.getContentType();
        if (contentType != null && contentType.contains(this.supportedContentType.getSubtype())) {

我认为我们需要对这个组件进行改进并不严格。与MappingJackson2MessageConverter通过它的方式相同:

/**
 * Whether this converter should convert messages for which no content type
 * could be resolved through the configured
 * {@link org.springframework.messaging.converter.ContentTypeResolver}.
 * <p>A converter can configured to be strict only when a
 * {@link #setContentTypeResolver contentTypeResolver} is configured and the
 * list of {@link #getSupportedMimeTypes() supportedMimeTypes} is not be empty.
 * <p>When this flag is set to {@code true}, {@link #supportsMimeType(MessageHeaders)}
 * will return {@code false} if the {@link #setContentTypeResolver contentTypeResolver}
 * is not defined or if no content-type header is present.
 */
public void setStrictContentTypeMatch(boolean strictContentTypeMatch) {

这是false默认情况下。

只有通过错过contentType克服问题的方法,但仍然继续JSON消息转换,我看到自定义org.springframework.amqp.support.converter.AbstractMessageConverter实现,你可以结合Jackson2JsonMessageConverter逻辑,而不关心错过contentType属性。

随意提出反对Spring AMQP的问题,以改善AbstractJackson2MessageConverter

© www.soinside.com 2019 - 2024. All rights reserved.