2

I am using remote MongoDB and connecting that with my Spring boot application. Application works fine if I define spring.data.mongodb.uri in my application.properties file, along with username and password. 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. I am using MongoOperations to query mongoDB.

I tried to create MongoDBTemplate in Configuration class, but it is throwing

com.mongodb.MongoSocketReadException: Prematurely reached end of stream

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);

    }

}
alepuzio
  • 1,382
  • 2
  • 28
  • 38
Sachin
  • 31
  • 1
  • 1
  • 5
  • https://stackoverflow.com/questions/39079876/mongosocketreadexception-prematurely-reached-end-of-stream-after-a-period-of-i – Vikki Jul 20 '18 at 14:50
  • I answered to a similar question here https://stackoverflow.com/questions/61289948/mongosocketreadexception-prematurely-reached-end-of-stream-while-connecting-mo/76868718#76868718 – Andrea Ciccotta Aug 09 '23 at 14:24

2 Answers2

1

I got the answer at How to connect to MongoDB 3.2 in Java with username and password?

I have added custom property in application.property file and then in AppConfig class created MongoClient object as below.

@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;
    }
    }
Alien
  • 15,141
  • 6
  • 37
  • 57
Sachin
  • 31
  • 1
  • 1
  • 5
1

This worked for me in spring boot with cloud based mongo (clustered instances).

Edit application.properties like this:

spring.data.mongodb.uri = mongodb+srv://username:password@solarsystem-1tpu0.mongodb.net/dbname

For regular mongo instances (non-clustered) use this:

spring.data.mongodb.uri = mongodb://username:password@solarsystem-shard-00-00-1tpu0.mongodb.net:27017,hostname2:27017/dbname?ssl=true
Community
  • 1
  • 1
julianm
  • 2,393
  • 1
  • 23
  • 24