美文网首页Spring Boot
Spring Boot MongoDB Map的Key包含点

Spring Boot MongoDB Map的Key包含点

作者: Qihang | 来源:发表于2017-08-31 17:55 被阅读433次

    spring boot contains dots but no replacement was configured! Make sure map keys don't contain dots in the first place or configure an appropriate replacement!

    • Spring Boot版本1.5.6.RELEASE
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-mongodb</artifactId>
    </dependency>
    
    • 源码类MappingMongoConverter.java
    protected String potentiallyEscapeMapKey(String source) {
        if(!source.contains(".")) {
            return source;
        } else if(this.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!", new Object[]{source}));
        } else {
            return source.replaceAll("\\.", this.mapKeyDotReplacement);
        }
    }
    
    • 解决方案
      • 方案一
        替换key中的.
        例如 1.5米 入库时改成1_5米
      • 方案二
        • 创建mongo.xml文件
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">
        <bean   id="mongoMoxydomainConverter"
            class="org.springframework.data.mongodb.core.convert.MappingMongoConverter">
        <constructor-arg index="0" ref="mongoDbFactory" />
        <constructor-arg index="1">
            <bean class="org.springframework.data.mongodb.core.mapping.MongoMappingContext"/>
        </constructor-arg>
        <property name="mapKeyDotReplacement" value="\\+"/>
        </bean>
    </beans>
    
    @SpringBootApplication
    @ImportResource("classpath:mongo.xml")
    

    相关文章

      网友评论

        本文标题:Spring Boot MongoDB Map的Key包含点

        本文链接:https://www.haomeiwen.com/subject/oefcjxtx.html