[英]com.mongodb.MongoSocketReadException: Prematurely reached end of stream
I am using remote MongoDB and connecting that with my Spring boot application. 我正在使用远程MongoDB,并将其与我的Spring启动应用程序连接。 Application works fine if I define spring.data.mongodb.uri in my application.properties file, along with username and password. 如果我在application.properties文件中定义spring.data.mongodb.uri以及用户名和密码,则应用程序运行良好。 Something similar to 类似于
spring.data.mongodb.uri = mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]
I wanted to keep encoded url in my application.property file and then want to decode it before use. 我想在我的application.property文件中保留编码的url,然后在使用前对其进行解码。 I am using MongoOperations to query mongoDB. 我正在使用MongoOperations查询mongoDB。
I tried to create MongoDBTemplate in Configuration class, but it is throwing 我试图在Configuration类中创建MongoDBTemplate,但是它抛出了
com.mongodb.MongoSocketReadException: Prematurely reached end of stream com.mongodb.MongoSocketReadException:过早到达流的结尾
Here's the code 这是代码
package com.expensemanagement.base;
import java.util.Arrays;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
@Configuration
public class AppConfig {
@Bean
public MongoTemplate mongoTemplate() throws Exception {
char[] password2 = "XXXX".toCharArray();
MongoCredential credential2 = MongoCredential.createCredential("XXXX", "MongoDB",password2);
MongoClient mongoClient = new MongoClient(new ServerAddress("XXX-XXX-XX-XX-XXX.mongodb.net",27017), Arrays.asList(credential2));
MongoDbFactory mongoDbFactory = new SimpleMongoDbFactory(mongoClient, "MongoDB");
return new MongoTemplate(mongoDbFactory);
}
}
I got the answer at How to connect to MongoDB 3.2 in Java with username and password? 我在如何使用用户名和密码连接Java中的MongoDB 3.2时得到了答案?
I have added custom property in application.property file and then in AppConfig class created MongoClient object as below. 我在application.property文件中添加了自定义属性,然后在AppConfig类中创建了MongoClient对象,如下所示。
@Configuration
public class AppConfig {
@Value("${mongo.url}")
private String url;
@Value("${application.key}")
private String secretKey;
private MongoClient mongoClient;
@Bean
public MongoClient mongoClient()
{
String decrptedUrl = DecryptionService.decrypt(url, secretKey);
MongoClientURI connectionString = new MongoClientURI(decrptedUrl);
if (this.mongoClient == null) {
this.mongoClient = new MongoClient(connectionString);
}
return mongoClient;
}
}
Edit application.properties like this: 像这样编辑application.properties:
spring.data.mongodb.uri = mongodb+srv://username:password@solarsystem-1tpu0.mongodb.net/dbname
For regular mongo instances (non-clustered) use this: 对于常规mongo实例 (非集群),请使用以下命令:
spring.data.mongodb.uri = mongodb://username:password@solarsystem-shard-00-00-1tpu0.mongodb.net:27017,hostname2:27017/dbname?ssl=true
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.