gpt4 book ai didi

spring - MongoDB-映射键中的转义点 '.']

转载 作者:行者123 更新时间:2023-12-02 10:00:01 27 4
gpt4 key购买 nike

产品的 map 键代码包含点,但未配置替换!首先确保 map 键不包含点或配置适当的替换!

org.springframework.data.mapping.model.MappingException: Map key foo.bar.key 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:622) at org.springframework.data.mongodb.core.convert.MappingMongoConverter.writeMapInternal(MappingMongoConverter.java:586) at org.springframework.data.mongodb.core.convert.MappingMongoConverter.createMap(MappingMongoConverter.java:517) at org.springframework.data.mongodb.core.convert.MappingMongoConverter.writePropertyInternal(MappingMongoConverter.java:424) at org.springframework.data.mongodb.core.convert.MappingMongoConverter$3.doWithPersistentProperty(MappingMongoConverter.java:386) at org.springframework.data.mongodb.core.convert.MappingMongoConverter$3.doWithPersistentProperty(MappingMongoConverter.java:373) at org.springframework.data.mapping.model.BasicPersistentEntity.doWithProperties(BasicPersistentEntity.java:257) at org.springframework.data.mongodb.core.convert.MappingMongoConverter.writeInternal(MappingMongoConverter.java:373) at org.springframework.data.mongodb.core.convert.MappingMongoConverter.writePropertyInternal(MappingMongoConverter.java:451) at org.springframework.data.mongodb.core.convert.MappingMongoConverter$3.doWithPersistentProperty(MappingMongoConverter.java:386) at org.springframework.data.mongodb.core.convert.MappingMongoConverter$3.doWithPersistentProperty(MappingMongoConverter.java:373) at org.springframework.data.mapping.model.BasicPersistentEntity.doWithProperties(BasicPersistentEntity.java:257) at org.springframework.data.mongodb.core.convert.MappingMongoConverter.writeInternal(MappingMongoConverter.java:373) at org.springframework.data.mongodb.core.convert.MappingMongoConverter.writePropertyInternal(MappingMongoConverter.java:451) at org.springframework.data.mongodb.core.convert.MappingMongoConverter$3.doWithPersistentProperty(MappingMongoConverter.java:386) at org.springframework.data.mongodb.core.convert.MappingMongoConverter$3.doWithPersistentProperty(MappingMongoConverter.java:373) at org.springframework.data.mapping.model.BasicPersistentEntity.doWithProperties(BasicPersistentEntity.java:257) at org.springframework.data.mongodb.core.convert.MappingMongoConverter.writeInternal(MappingMongoConverter.java:373) at org.springframework.data.mongodb.core.convert.MappingMongoConverter.writeInternal(MappingMongoConverter.java:345) at org.springframework.data.mongodb.core.convert.MappingMongoConverter.write(MappingMongoConverter.java:310) at org.springframework.data.mongodb.core.convert.MappingMongoConverter.write(MappingMongoConverter.java:77) at org.springframework.data.mongodb.core.MongoTemplate.doSave(MongoTemplate.java:859) at org.springframework.data.mongodb.core.MongoTemplate.save(MongoTemplate.java:806) at org.springframework.data.mongodb.core.MongoTemplate.save(MongoTemplate.java:794)

当我们尝试插入值时,就会发生这种情况。我们该如何解决这个问题?

这是我的课

@Configuration
@EnableMongoRepositories("net.ooo.hepsiburada.**.repository")
@Profile(Constants.SPRING_PROFILE_CLOUD)
public class CloudMongoDbConfiguration extends AbstractMongoConfiguration {

private final Logger log = LoggerFactory.getLogger(CloudDatabaseConfiguration.class);

@Inject
private MongoDbFactory mongoDbFactory;

@Bean
public ValidatingMongoEventListener validatingMongoEventListener() {
return new ValidatingMongoEventListener(validator());
}

@Bean
public LocalValidatorFactoryBean validator() {
return new LocalValidatorFactoryBean();
}

@Bean
public CustomConversions customConversions() {
List<Converter<?, ?>> converterList = new ArrayList<>();;
converterList.add(DateToZonedDateTimeConverter.INSTANCE);
converterList.add(ZonedDateTimeToDateConverter.INSTANCE);
converterList.add(DateToLocalDateConverter.INSTANCE);
converterList.add(LocalDateToDateConverter.INSTANCE);
converterList.add(DateToLocalDateTimeConverter.INSTANCE);
converterList.add(LocalDateTimeToDateConverter.INSTANCE);
return new CustomConversions(converterList);
}

@Override
protected String getDatabaseName() {
return mongoDbFactory.getDb().getName();
}

@Override
public Mongo mongo() throws Exception {
return mongoDbFactory().getDb().getMongo();
}
}

最佳答案

使用 Spring Data MongoDB 时,您将获得一个实例:org.springframework.data.mongodb.core.convert.MappingMongoConverter,默认情况下,mapKeyDotReplacement 设置为 null -这就是为什么你会得到异常(exception)。

您需要创建自己的 org.springframework.data.mongodb.core.convert.MappingMongoConverter 实例,或者仅使用其提供程序 setter 方法修改现有实例:

/**
* 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
*/
public void setMapKeyDotReplacement(String mapKeyDotReplacement) {
this.mapKeyDotReplacement = mapKeyDotReplacement;
}

在 MongoDB 中,点始终被视为特殊字符,因此避免使用它很可能会在将来为您省去一些其他麻烦。

编辑:要覆盖默认的 MappingMongoConverter,请添加以下 bean 声明:

  @Bean
public MappingMongoConverter mongoConverter(MongoDbFactory mongoFactory) throws Exception {
DbRefResolver dbRefResolver = new DefaultDbRefResolver(mongoFactory);
MappingMongoConverter mongoConverter = new MappingMongoConverter(dbRefResolver, mongoMappingContext);
mongoConverter.setMapKeyDotReplacement(".");

return mongoConverter;
}

关于spring - MongoDB-映射键中的转义点 '.'],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39785004/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com