当前位置: 首页 > 知识库问答 >
问题:

将Springboot从2.1.0迁移到2.2.1时出现Mongo错误

楚皓君
2023-03-14

从2.1.0迁移的Springboot版本2.2.1

@Configuration
@EnableMongoRepositories(basePackages = "repositories", mongoTemplateRef = "mongoOperations")
public class MongoConfiguration {

    private final MongoClientOptions options;
    private final MongoClientFactory factory;

    public MongoConfiguration(MongoProperties properties,
                                  ObjectProvider<MongoClientOptions> options, Environment environment) {
        this.options = options.getIfAvailable();
        this.factory = new MongoClientFactory(properties, environment);
    }

    @Bean(destroyMethod = "close")
    public MongoClient mongo() {
        return this.factory.createMongoClient(this.options);
    }

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

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

    @Bean
    public MongoOperations mongoOperations(final ShareContextMongoDbFactory mongoDbFactory, final MongoConverter mongoConverter) {
        return new ApplicationsMongoTemplate(mongoDbFactory, mongoConverter);
    }

    @Bean
    public ShareContextMongoDbFactory mongoDbFactory(final MongoClient mongoClient, final MongoProperties mongoProperties) {
        return new ShareContextMongoDbFactory(mongoClient, mongoProperties.getDatabase());
    }

}
public class ApplicationsMongoTemplate extends MultiTenancyMongoTemplate implements
    ApplicationEventPublisherAware {

  private ApplicationEventPublisher applicationEventPublisher;

  public ApplicationsMongoTemplate(
      ShareContextMongoDbFactory mongoDbFactory,
      MongoConverter mongoConverter) {
    super(mongoDbFactory, mongoConverter);
  }

  /**
   * Remove the given object from the collection by id.
   *
   * @param object must not be {@literal null}.
   * @return the {@link DeleteResult} which lets you access the results of the previous delete.
   */

  @Override
  @NonNull
  public DeleteResult remove(Object object) {
    Assert.notNull(object, "Object must not be null!");
    applicationEventPublisher.publishEvent(new BeforeDeleteEvent(object));
    return super.remove(object);
  }

  @Override
  public void setApplicationEventPublisher(
      @NonNull ApplicationEventPublisher applicationEventPublisher) {
    this.applicationEventPublisher = applicationEventPublisher;
  }
}

build.gradle dependecies

dependencies {
    compile "org.springframework.cloud:spring-cloud-starter-openfeign"
    compile "org.springframework.boot:spring-boot-starter-cache"
    compile "org.springframework.boot:spring-boot-starter-actuator"
    compile "org.springframework.boot:spring-boot-starter-data-mongodb"
    compile "org.springframework.boot:spring-boot-starter-security"
    compile "org.springframework.boot:spring-boot-starter-validation"
    compile "org.springframework.boot:spring-boot-starter-websocket"
    compile "org.springframework.boot:spring-boot-starter-web"
    compile "org.springframework.boot:spring-boot-starter-aop"
    compile "org.springframework.boot:spring-boot-starter-amqp"
    compileOnly "org.springframework.boot:spring-boot-configuration-processor"
    testCompile "org.springframework.boot:spring-boot-starter-test"
    testCompile "org.openjdk.jmh:jmh-core:${jmh_version}"
    testCompile "org.openjdk.jmh:jmh-generator-annprocess:${jmh_version}"
    testCompile "org.powermock:powermock-module-junit4:${powermock_version}"
    testCompile "org.powermock:powermock-api-mockito2:${powermock_version}"
    testCompile "org.mockito:mockito-core:2.18.3"
    testCompile "commons-beanutils:commons-beanutils:1.9.4"
}

共有1个答案

范书
2023-03-14

从日志中,我发现在spring managed mongo文档中没有正确定义索引。尝试检查ensureIndex方法和@CompoundIndex注释。

 类似资料:
  • 你能帮助我们哪里错了或者需要做什么吗… 提前感谢…

  • 我基本上是在Crashlytics中遵循迁移指南(这篇文章对其进行了总结)。 我一字不差地遵循相同的说明,您可以从我的中看到: 然而,当我同步我的Gradle时,我得到了这个错误: 5:02 PM Gradle同步失败:找不到DefaultDependencyHandler类型org.gradle.api.internal.artifacts.dsl.dependencies.对象的参数[com.

  • 我已经将play framework从2.0.8迁移到2.1.0,以前一切都很好。现在我得到以下错误: play.api.expectiontedException:意外异常[persistenceException:[persistenceUnit:hbllnd]无法在play.core.reloadableApplication$$AnonFun$Get$1$$AnonFun$1构建Entit

  • 我正在尝试将Mockito测试从Junit 4迁移到Junit 5,方法是将替换为,但这会导致以下错误。不知道我错过了什么,任何帮助都将非常感谢。 测试类:- 从Intellij运行测试时的错误日志:-

  • 当我尝试启动我的应用程序时,我的 MainActivity 在 oncreate() 方法中的“setContentView(R.layout.activity_main);” 中抛出此错误: 这个错误出现在迁移到androidx之后,经过进一步检查,我注意到Bottomnavigationview类中的导入语句已经过时(没有androidx格式,而是旧格式)。Bottomnavigationvi

  • MongoDB从3.2转储,用3.4还原,错误索引保存=null 在我的情况下,手工重新创建索引不是一个选项,我需要一个脚本来自动创建索引,以便以后迁移我的生产环境。 到目前为止我所尝试的: 正确的进行方式是什么?