问题
If it's not one problem, it's another... I seem to have gotten the 2 previous problems fixed but now when running outside of a test context, with a stood up application I see this.
o.s.a.s.c.Jackson2JsonMessageConverter : Could not convert incoming message with content-type [null], 'json' keyword missing.
Like before, this message seems fairly clear. Heck, things even worked in a standalone test context, however when running in a standalone server the code seems to be taking a different path and is failing for the same reason but via a different component.
@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
}
}
}
回答1:
You can add a MessagePostprocessor
to the container factory to enhance the message with a content_id property.
factory.setAfterReceivePostProcessors(m -> {
m.getMessageProperties().setContentId("application/json");
return m;
}
回答2:
According the logic in the AbstractJackson2MessageConverter
, the contentType
AMQP property is required:
MessageProperties properties = message.getMessageProperties();
if (properties != null) {
String contentType = properties.getContentType();
if (contentType != null && contentType.contains(this.supportedContentType.getSubtype())) {
I think we need to bring an improvement into this component do not be strict with it. The same way as we can do with the MappingJackson2MessageConverter
via its:
/**
* 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) {
which is false
by default.
Only the way to overcome the issue with missed contentType
, but still proceed with the JSON message conversion, I see with a custom org.springframework.amqp.support.converter.AbstractMessageConverter
implementation where you can combine a Jackson2JsonMessageConverter
logic and don't care about missed contentType
property.
Feel free to raise an issue against Spring AMQP to improve an AbstractJackson2MessageConverter
!
来源:https://stackoverflow.com/questions/55093859/how-do-i-handle-content-type-null-with-jackson2jsonmessageconverter-in-spring-am