不再支持将 UserCredentials 与 MongoClient 一起使用

Usage of UserCredentials with MongoClient is no longer supported

我正在尝试将我的 spring 启动应用程序部署到 Cloud Foundry。但是我收到以下错误。

2016-02-19T16:54:29.57+0000 [App/0]      ERR Caused by: org.springframework.dao.InvalidDataAccessApiUsageException: Usage of 'UserCredentials' with 'MongoClient' is no longer supported. Please use 'MongoCredential' for 'MongoClient' or just 'Mongo'.
2016-02-19T16:54:29.57+0000 [App/0]      ERR    at org.springframework.data.mongodb.core.SimpleMongoDbFactory.<init>(SimpleMongoDbFactory.java:137)
2016-02-19T16:54:29.57+0000 [App/0]      ERR    at org.springframework.data.mongodb.core.SimpleMongoDbFactory.<init>(SimpleMongoDbFactory.java:78)
2016-02-19T16:54:29.57+0000 [App/0]      ERR    at org.cloudfoundry.reconfiguration.org.springframework.cloud.service.document.MongoDbFactoryCreator.create(MongoDbFactoryCreator.java:46)
2016-02-19T16:54:29.57+0000 [App/0]      ERR    at org.cloudfoundry.reconfiguration.org.springframework.cloud.service.document.MongoDbFactoryCreator.create(MongoDbFactoryCreator.java:35)
2016-02-19T16:54:29.57+0000 [App/0]      ERR    at org.cloudfoundry.reconfiguration.org.springframework.cloud.Cloud.getServiceConnector(Cloud.java:257)
2016-02-19T16:54:29.57+0000 [App/0]      ERR    at org.cloudfoundry.reconfiguration.org.springframework.cloud.Cloud.getSingletonServiceConnector(Cloud.java:167)
2016-02-19T16:54:29.57+0000 [App/0]      ERR    at org.cloudfoundry.reconfiguration.spring.AbstractCloudServiceBeanFactoryPostProcessor.reconfigureBean(AbstractCloudServiceBeanFactoryPostProcessor.java:119)

据我所知,Spring Boot 会自动找到绑定的 Mongo 服务和所有必要的用户凭据和 URI 详细信息,这意味着我不需要显式声明这些变量。

有谁知道我为什么收到这个错误以及我该如何解决它?

这是我的 Java 代码片段:

@Autowired
    public MongoRepository(MongoClient mongo) {
        this.mongo = mongo;
    }


public long insert(Document document){

    MongoDatabase db = mongo.getDatabase("test");
    MongoCollection<Document> coll = db.getCollection("document");
    coll.insertOne(document);
}

我正在使用 Mongo3.0 java 驱动程序。

<dependency>
    <groupId>org.mongodb</groupId>
    <artifactId>mongo-java-driver</artifactId>
    <version>3.0.0</version><!--$NO-MVN-MAN-VER$-->
</dependency>

您也依赖 Cloud Foundry Java buildpack auto-configuration to configure the MongoDB connection. The Java buildpack depends on Spring Cloud Connectors, Spring Data MongoDB, and the MongDB client library to create the necessary connection beans. The MongoDB client made some changes a while ago that required Spring Data MongDB and Spring Cloud Connectors 进行更改。您使用的 Java Buildpack 似乎依赖于这些库的旧版本。

您有几个选择。

您可以使用更新版本的 Java buildpack 来推送您的应用程序。看来 JBP 的 version 3.2 及更高版本具有满足您需要的正确库。

或者,您可以在您的应用程序中明确包含 Spring Cloud Connectors 1.2.0 或更高版本,这会有效地禁用 Java buildpack 自动配置。有关此方法的更多信息,请参阅 Connectors docs

我没有使用 Spring 引导,但我也遇到了这个错误。我通过使用以下配置解决了这个问题:

@Configuration
public class SpringMongoConfig {

    public @Bean MongoClient mongoClient() {
        return new MongoClient(new ServerAddress("127.0.0.1", 27017), new ArrayList<MongoCredential>() {

            {
                add(MongoCredential.createCredential("username", "dbname", "pwd".toCharArray()));
            }
        });
    }

    public @Bean MongoDbFactory mongoDbFactory() throws Exception {
        return new SimpleMongoDbFactory(mongoClient(), "dbname");
    }

    public @Bean MongoTemplate mongoTemplate() throws Exception {
        MongoTemplate mongoTemplate = new MongoTemplate(mongoDbFactory());
        return mongoTemplate;
    }

}