1

I am new and I try to develop an API with Java Spring Boot. I have a problem with MongoDB Config and I have no problem in another project. I don't understand why. I have already looked but the solutions proposed do not work.

SBApplication.java :

package basket;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.data.mongo.MongoRepositoriesAutoConfiguration;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration;

import springfox.documentation.swagger2.annotations.EnableSwagger2;

@SpringBootApplication (scanBasePackages = { "basket.instrastructure.dao" })
@EnableSwagger2
@EnableAutoConfiguration //(exclude={MongoAutoConfiguration.class, MongoRepositoriesAutoConfiguration.class})
public class SBApplication {

    public static void main(String[] args) {
        SpringApplication.run(SBApplication.class, args);
    }
}

BasketRepository.java :

package basket.infrastructure.dao;

import java.util.ArrayList;

import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;

import basket.domaine.model.Basket;

@Repository 
public interface BasketRepository extends MongoRepository<Basket, Long> {

    public ArrayList<Basket> findAll();
    public Basket findById(String id);    
}

ItemRepository.java :

package basket.infrastructure.dao;

import java.util.ArrayList;

import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;

import basket.domaine.model.Item;


@Repository
public interface ItemRepository extends MongoRepository<Item, Long> {
    public Item findByName(String name);
    public ArrayList<Item> findAll();
    public Item findById(String id);
}

MongoConfig.java :

package basket.application.components;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.config.AbstractMongoConfiguration;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;

import com.mongodb.Mongo;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.client.MongoDatabase;

@Configuration
@EnableMongoRepositories ("basket.infrastructure.dao")
public class MongoConfig extends  AbstractMongoConfiguration {

    @Override
    @Bean
    public Mongo mongo() throws Exception {
        MongoClientURI uri = new MongoClientURI("mongodb+srv://owner:[email protected]/test?retryWrites=true");
        MongoClient mongoClient = new MongoClient(uri);
        MongoDatabase database = mongoClient.getDatabase("test");
        return mongoClient;
    }

    @Override
    protected String getDatabaseName() {
        return "test";
    }
}

And I have this error

com.mongodb.MongoSocketOpenException: Exception opening socket
    at com.mongodb.connection.SocketStream.open(SocketStream.java:63) ~[mongodb-driver-core-3.4.2.jar:na]
    at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:115) ~[mongodb-driver-core-3.4.2.jar:na]
    at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:113) ~[mongodb-driver-core-3.4.2.jar:na]
    at java.lang.Thread.run(Thread.java:748) [na:1.8.0_181]
Caused by: java.net.ConnectException: Connection refused (Connection refused)
    at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.8.0_181]
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_181]
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_181]
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_181]
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_181]
    at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_181]
    at com.mongodb.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:57) ~[mongodb-driver-core-3.4.2.jar:na]
    at com.mongodb.connection.SocketStream.open(SocketStream.java:58) ~[mongodb-driver-core-3.4.2.jar:na]
    ... 3 common frames omitted

Can you help me to understand my mistake. Thank you, any help is very much appreciated!

2
  • you forgot to add the port in your url Commented Nov 19, 2018 at 22:44
  • You can refer this, paulsofts.com/… Commented Apr 9, 2023 at 6:24

3 Answers 3

2

Are you sure about your MongoDB URI? Usually, the URI has the form mongodb://localhost:27017/foo.

If you use Spring-boot instead of using a Configuration bean I suggest you use application.properties file. In this file write a property likes spring.data.mongodb.uri=mongodb://localhost:27017/foo and Spring-boot automatically injects the properties in MongoClient without any configuration at the code level.

Sign up to request clarification or add additional context in comments.

1 Comment

I will test with the application.porperties file I had a project in which I had no problem but I redid with DDD architecture. And now I have the problem. The database is on the server mongoDB. And in the doc I can connect to the data base with : MongoClientURI uri = new MongoClientURI( "mongodb+srv://kay:[email protected]/"); MongoClient mongoClient = new MongoClient(uri); MongoDatabase database = mongoClient.getDatabase("test");
0

Try as @Federico Gatti said by configuring Mongo iuth properties file:

spring.data.mongodb.database=MongoDatabaseName
spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017

Comments

0

Use properties file to configure your database with application and let the spring boot do everything for you with db configuration.

Go to the resources/application.properties file, and add the following content to it:

spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.database=mydatabase

That will allow your application to correctly connect to your MongoDB database.

Also, Go to the main application class, and add the following annotation before its declaration:

@EnableMongoRepositories(basePackageClasses = foo.class)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.