org.springframework.data.mapping.MappingException: Map key xxx.yyyyy contains dots but no replacemen

org.springframework.data.mapping.MappingException: Map key xxx.yyyyy contains dots but no replacement was configured!

当map中key包含 "." 时,默认会抛出异常,需要显示的配置替换策略,可以在数据源配置里设置mapKeyDotReplacement的值

org.springframework.data.mapping.MappingException

mongoTemplate.save(map, "collectionName");
/**
 * 当map中key包含 "." 时,默认会抛出异常
 * org.springframework.data.mapping.MappingException: Map key xxx.yyyyy contains dots but no replacement was configured!
 */
org.springframework.data.mapping.MappingException: Map key xxx.yyyyy 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:760) ~[spring-data-mongodb-2.0.6.RELEASE.jar:2.0.6.RELEASE]
	at org.springframework.data.mongodb.core.convert.MappingMongoConverter.prepareMapKey(MappingMongoConverter.java:742) ~[spring-data-mongodb-2.0.6.RELEASE.jar:2.0.6.RELEASE]
	at org.springframework.data.mongodb.core.convert.MappingMongoConverter.writeMapInternal(MappingMongoConverter.java:709) ~[spring-data-mongodb-2.0.6.RELEASE.jar:2.0.6.RELEASE]
	at org.springframework.data.mongodb.core.convert.MappingMongoConverter.writeInternal(MappingMongoConverter.java:426) ~[spring-data-mongodb-2.0.6.RELEASE.jar:2.0.6.RELEASE]
	at org.springframework.data.mongodb.core.convert.MappingMongoConverter.writeMapInternal(MappingMongoConverter.java:719) ~[spring-data-mongodb-2.0.6.RELEASE.jar:2.0.6.RELEASE]
	at org.springframework.data.mongodb.core.convert.MappingMongoConverter.writeInternal(MappingMongoConverter.java:426) ~[spring-data-mongodb-2.0.6.RELEASE.jar:2.0.6.RELEASE]
	at org.springframework.data.mongodb.core.convert.MappingMongoConverter.writeMapInternal(MappingMongoConverter.java:719) ~[spring-data-mongodb-2.0.6.RELEASE.jar:2.0.6.RELEASE]
	at org.springframework.data.mongodb.core.convert.MappingMongoConverter.writeInternal(MappingMongoConverter.java:426) ~[spring-data-mongodb-2.0.6.RELEASE.jar:2.0.6.RELEASE]
	at org.springframework.data.mongodb.core.convert.MappingMongoConverter.writeMapInternal(MappingMongoConverter.java:719) ~[spring-data-mongodb-2.0.6.RELEASE.jar:2.0.6.RELEASE]
	at org.springframework.data.mongodb.core.convert.MappingMongoConverter.writeInternal(MappingMongoConverter.java:426) ~[spring-data-mongodb-2.0.6.RELEASE.jar:2.0.6.RELEASE]
	at org.springframework.data.mongodb.core.convert.MappingMongoConverter.write(MappingMongoConverter.java:391) ~[spring-data-mongodb-2.0.6.RELEASE.jar:2.0.6.RELEASE]
	at org.springframework.data.mongodb.core.convert.MappingMongoConverter.write(MappingMongoConverter.java:86) ~[spring-data-mongodb-2.0.6.RELEASE.jar:2.0.6.RELEASE]
	at org.springframework.data.mongodb.core.MongoTemplate.toDocument(MongoTemplate.java:1070) ~[spring-data-mongodb-2.0.6.RELEASE.jar:2.0.6.RELEASE]
	at org.springframework.data.mongodb.core.MongoTemplate.doSave(MongoTemplate.java:1253) ~[spring-data-mongodb-2.0.6.RELEASE.jar:2.0.6.RELEASE]
	at org.springframework.data.mongodb.core.MongoTemplate.save(MongoTemplate.java:1201) ~[spring-data-mongodb-2.0.6.RELEASE.jar:2.0.6.RELEASE]
	at com.paic.biz.docker.work.ServerStatusService$1.handleDelivery(ServerStatusService.java:83) [haiyoung-api-biz-0.0.1-SNAPSHOT.jar:?]
	at com.rabbitmq.client.impl.ConsumerDispatcher$5.run(ConsumerDispatcher.java:149) [amqp-client-5.1.2.jar:5.1.2]
	at com.rabbitmq.client.impl.ConsumerWorkService$WorkPoolRunnable.run(ConsumerWorkService.java:100) [amqp-client-5.1.2.jar:5.1.2]
	at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [?:1.8.0_172]
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [?:1.8.0_172]
	at java.lang.Thread.run(Unknown Source) [?:1.8.0_172]

MappingMongoConverter源码

org.springframework.data.mongodb.core.convert.MappingMongoConverter

protected @Nullable String mapKeyDotReplacement = null;
/**
 * Configure the characters dots potentially contained in a {@link Map} shall be replaced with. By default we don't do
 * any translation but rather reject a {@link Map} with keys containing dots causing the conversion for the entire
 * object to fail. If further customization of the translation is needed, have a look at
 * {@link #potentiallyEscapeMapKey(String)} as well as {@link #potentiallyUnescapeMapKey(String)}.
 *
 * @param mapKeyDotReplacement the mapKeyDotReplacement to set. Can be {@literal null}.
 */
public void setMapKeyDotReplacement(@Nullable String mapKeyDotReplacement) {
    this.mapKeyDotReplacement = mapKeyDotReplacement;
}

/**
 * Potentially replaces dots in the given map key with the configured map key replacement if configured or aborts
 * conversion if none is configured.
 *
 * @see #setMapKeyDotReplacement(String)
 * @param source
 * @return
 */
protected String potentiallyEscapeMapKey(String source) {

    if (!source.contains(".")) {
        return source;
    }

    if (mapKeyDotReplacement == null) {
        throw new MappingException(String.format(
                "Map key %s contains dots but no replacement was configured! Make "
                        + "sure map keys don't contain dots in the first place or configure an appropriate replacement!",
                source));
    }

    return source.replaceAll("\\.", mapKeyDotReplacement);
}

/**
 * Translates the map key replacements in the given key just read with a dot in case a map key replacement has been
 * configured.
 *
 * @param source
 * @return
 */
protected String potentiallyUnescapeMapKey(String source) {
    return mapKeyDotReplacement == null ? source : source.replaceAll(mapKeyDotReplacement, "\\.");
}

mongo数据源配置



    






    




    
    
    
    
    



新增 如下属性配置之后,异常消失,数据成功落地

你可能感兴趣的:(Spring,mongo,database)