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

时间:2019-03-11 00:39:38

标签: java spring kotlin spring-amqp

如果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
        }
    }
}

2 个答案:

答案 0 :(得分:2)

您可以向容器工厂添加string.Format,以使用content_id属性增强消息。

MessagePostprocessor

答案 1 :(得分: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