我正在使用MongoDbMessageStore存储传入的AMQP消息。消息具有标头,其键包含点。当消息将要保留在MongoDB中时,将引发异常:

org.springframework.data.mapping.model.MappingException: Map key lnu_ocr_core.ocr.rabbitmq_ver contains dots but no replacement was configured! Make sure map keys don't contain dots in the first place or configure an appropriate replacement!
    at org.springframework.data.mongodb.core.convert.MappingMongoConverter.potentiallyEscapeMapKey(MappingMongoConverter.java:714)
    at org.springframework.data.mongodb.core.convert.MappingMongoConverter.prepareMapKey(MappingMongoConverter.java:696)
    at org.springframework.data.mongodb.core.convert.MappingMongoConverter.writeMapInternal(MappingMongoConverter.java:663)
    at org.springframework.data.mongodb.core.convert.MappingMongoConverter.createMap(MappingMongoConverter.java:593)
    at org.springframework.data.mongodb.core.convert.MappingMongoConverter.writePropertyInternal(MappingMongoConverter.java:478)
    at org.springframework.data.mongodb.core.convert.MappingMongoConverter$3.doWithPersistentProperty(MappingMongoConverter.java:437)
    at org.springframework.data.mongodb.core.convert.MappingMongoConverter$3.doWithPersistentProperty(MappingMongoConverter.java:425)
    at org.springframework.data.mapping.model.BasicPersistentEntity.doWithProperties(BasicPersistentEntity.java:330)
    at org.springframework.data.mongodb.core.convert.MappingMongoConverter.writeInternal(MappingMongoConverter.java:425)
    at org.springframework.data.mongodb.core.convert.MappingMongoConverter.writeInternal(MappingMongoConverter.java:399)
    at org.springframework.data.mongodb.core.convert.MappingMongoConverter.write(MappingMongoConverter.java:363)
    at org.springframework.integration.mongodb.store.MongoDbMessageStore$MessageReadingMongoConverter.write(MongoDbMessageStore.java:530)
    at org.springframework.integration.mongodb.store.MongoDbMessageStore$MessageReadingMongoConverter.write(MongoDbMessageStore.java:497)
    at org.springframework.data.mongodb.core.MongoTemplate.toDbObject(MongoTemplate.java:873)
    at org.springframework.data.mongodb.core.MongoTemplate.doInsert(MongoTemplate.java:855)
    at org.springframework.data.mongodb.core.MongoTemplate.insert(MongoTemplate.java:799)
    at org.springframework.integration.mongodb.store.MongoDbMessageStore.addMessageDocument(MongoDbMessageStore.java:228)
    at org.springframework.integration.mongodb.store.MongoDbMessageStore.addMessagesToGroup(MongoDbMessageStore.java:303)
    at org.springframework.integration.store.AbstractMessageGroupStore.addMessageToGroup(AbstractMessageGroupStore.java:209)
    at org.springframework.integration.aggregator.AbstractCorrelatingMessageHandler.store(AbstractCorrelatingMessageHandler.java:621)
    at org.springframework.integration.aggregator.AbstractCorrelatingMessageHandler.handleMessageInternal(AbstractCorrelatingMessageHandler.java:413)
    at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:127)
    ...


我知道要解决此问题,必须在mapKeyDotReplacement上设置MappingMongoConverter。但是,MongoDbMessageStore使用内部创建和配置的MongoDbMessageStore.MessageReadingMongoConverter私有实例。有没有办法在其中设置mapKeyDotReplacement

最佳答案

您必须放弃不方便的MongoDbMessageStore,而转而使用ConfigurableMongoDbMessageStore。从语义上讲,它们是相同的,并且可以使用外部MappingMongoConverter进行配置:

public ConfigurableMongoDbMessageStore(MongoDbFactory mongoDbFactory, MappingMongoConverter mappingMongoConverter) {


https://docs.spring.io/spring-integration/docs/5.0.0.RELEASE/reference/html/mongodb.html#mongodb-message-store

10-07 21:19