版权声明:本文为博主原创文章,转载请注明出处。 https://blog.csdn.net/haiyoung/article/details/81228043
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数据源配置
<!-- mongo.haiyoung -->
<mongo:mongo-client id="mongoClient_haiyoung" replica-set="${mongo.replica-set}" credentials="${mongo.user}:${mongo.pwd}@${mongo.defaultDbName}">
<mongo:client-options connections-per-host="${mongo.connectionsPerHost}"
min-connections-per-host="${mongo.minConnectionsPerHost}"
threads-allowed-to-block-for-connection-multiplier="${mongo.threadsAllowedToBlockForConnectionMultiplier}"
connect-timeout="${mongo.connectTimeout}" max-wait-time="${mongo.maxWaitTime}"
socket-keep-alive="${mongo.socketKeepAlive}" socket-timeout="${mongo.socketTimeout}"
description="${mongo.description}" max-connection-idle-time="${mongo.maxConnectionIdleTime}"
max-connection-life-time="${mongo.maxConnectionLifeTime}"
heartbeat-socket-timeout="${mongo.heartbeatSocketTimeout}"
heartbeat-connect-timeout="${mongo.heartbeatConnectTimeout}"
min-heartbeat-frequency="${mongo.minHeartbeatFrequency}"
heartbeat-frequency="${mongo.heartbeatFrequency}"
write-concern="${mongo.write-concern}" read-preference="${mongo.read-preference}" />
</mongo:mongo-client>
<mongo:db-factory id="mongoDbFactory_haiyoung" dbname="${mongo.defaultDbName}" mongo-ref="mongoClient_haiyoung" />
<!--去除自动添加的 _class 字段-->
<bean id="defaultMongoTypeMapper" class="org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper">
<constructor-arg name="typeKey"><null /></constructor-arg>
</bean>
<bean id="mappingContext" class="org.springframework.data.mongodb.core.mapping.MongoMappingContext" />
<bean id ="mappingMongoConverter_haiyoung" class="org.springframework.data.mongodb.core.convert.MappingMongoConverter">
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory_haiyoung"/>
<constructor-arg name="mappingContext" ref="mappingContext"/>
<property name="typeMapper" ref="defaultMongoTypeMapper"></property>
<!--设置对 dot 的替换规则-->
<property name="mapKeyDotReplacement" value="@dot@"></property>
</bean>
<!-- 1.Spring提供的mongodb操作模板 -->
<mongo:template id="mongoTemplate" db-factory-ref="mongoDbFactory_haiyoung" converter-ref="mappingMongoConverter_haiyoung" />
新增 如下属性配置之后,异常消失,数据成功落地
<property name="mapKeyDotReplacement" value="@dot@"></property>