简体   繁体   English

com.mongodb.MongoSocketReadException:过早到达流的结尾

[英]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;
    }
    }

This worked for me in spring boot with cloud based mongo (clustered instances). 在春季启动时,这对我基于云的mongo(集群实例)有效。

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.

相关问题 mongoDB和Spark:“ com.mongodb.MongoSocketReadException:过早到达流的末尾” - mongoDB & Spark: “com.mongodb.MongoSocketReadException: Prematurely reached end of stream” 将Java应用程序连接到MongoDB-com.mongodb.MongoSocketReadException:已提前到达流的末尾 - Connect Java- Application to MongoDB - com.mongodb.MongoSocketReadException: Prematurely reached end of stream 获取com.mongodb.MongoSocketReadException:过早地到达流MongoDB的末尾 - Getting com.mongodb.MongoSocketReadException: Prematurely reached end of stream- MongoDB MongoClientURI 连接字符串抛出错误“com.mongodb.MongoSocketReadException:过早到达流的末尾” - MongoClientURI connection string throwing error “com.mongodb.MongoSocketReadException: Prematurely reached end of stream” 如何修复 com.mongo.MongoSocketReadException 并在尝试插入文档时出现消息“过早到达 Stream 的末尾”? - How do I fix a com.mongo.MongoSocketReadException, with the message "Prematurely reached the end of Stream" on attempting to insert a document? MongoSocketReadException:在 Spring 引导连接 MongoDB Atlas 时,过早到达 stream 的末尾 - MongoSocketReadException :Prematurely reached end of stream ,while connecting MongoDB Atlas by Spring Boot MongoSocketReadException:过早到达流末尾(使用 ssl 从 Java 到 Mongo) - MongoSocketReadException: Prematurely reached end of stream (Java to Mongo using ssl) MongoSocketReadException:过早到达 stream 的末尾(一段时间不活动后) - MongoSocketReadException: Prematurely reached end of stream (after a period of inactivity) 什么是“com.mongodb.MongoSocketReadException”和“com.mongodb.MongoTimeoutException”的解决方案 - What is the solution "com.mongodb.MongoSocketReadException" and "com.mongodb.MongoTimeoutException" 引起原因:com.mongodb.MongoSocketReadException:异常接收消息 - Caused by: com.mongodb.MongoSocketReadException: Exception receiving message
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM