SpringBoot-data-MongoDB 报错Please use ‘MongoMappingContext#setAutoIndexCreation(boolean)‘

Please use ‘MongoMappingContext#setAutoIndexCreation(boolean)’ or override ‘MongoConfigurationSupport#autoIndexCreation()’ to be explicit.

​ However, we recommend setting up indices manually in an application ready block. You may use index derivation there as well.

> -----------------------------------------------------------------------------------------
> @EventListener(ApplicationReadyEvent.class)
> public void initIndicesAfterStartup() {
    
    
>
>     IndexOperations indexOps = mongoTemplate.indexOps(DomainType.class);
>
>     IndexResolver resolver = new MongoPersistentEntityIndexResolver(mongoMappingContext);
>     resolver.resolveIndexFor(DomainType.class).forEach(indexOps::ensureIndex);
> }
> -----------------------------------------------------------------------------------------

1、看提示的原因是springBoot2.2.0之后自动创建index不再被推荐使用

2、在message类中默认自动创建index,所以导致了报错

在这里插入图片描述

3、解决办法

在yml文件中添加

spring:
  data:
    mongodb:
      auto-index-creation: false

这样就没问题了[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-hylO280D-1603458153949)(wenti.assets/image-20201023210149336.png)]

猜你喜欢

转载自blog.csdn.net/qq_45372719/article/details/109249832