Hi i'm trying to run my spring boot application connected with mongo, it starts and connects to the database but as soon as I make a request from insomnia it throws these errors:
2023-05-24T10:46:37.023+02:00 INFO 21568 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2023-05-24T10:46:37.024+02:00 INFO 21568 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2023-05-24T10:46:37.025+02:00 INFO 21568 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 0 ms
2023-05-24T10:46:37.207+02:00 INFO 21568 --- [nio-8080-exec-1] org.mongodb.driver.cluster : Cluster description not yet available. Waiting for 30000 ms before timing out
2023-05-24T10:47:07.222+02:00 ERROR 21568 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting to connect. Client view of cluster state is {type=UNKNOWN, servers=[{address=192.168.3.80:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: Connection refused: no further information}}]] with root cause
com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting to connect. Client view of cluster state is {type=UNKNOWN, servers=[{address=192.168.3.80:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: Connection refused: no further information}}]
These are my config:
@Configuration
public class MongoConfig {
@Value("${mongo.host}")
private String connectionString;
@Value("${mongo.db}")
private String db;
@Value("${mongo.collection}")
private String collection;
@Bean
public MongoCollection<Document> collection() {
MongoClient mongoClient = create(connectionString);
return mongoClient.getDatabase(db).getCollection(collection);
}
properties:
mongo:
host: ${DB_URL:mongodb://"myIpAddress":27017}
username:
pass:
db: db
collection: musica
server.port: 8080
server:
error:
include-message: always
include-binding-errors: always
include-stacktrace: never
include-exception: false
I tried changing drivers version, checked my firewall, added auto configuration and nothing worked. Fun fact: my app works perfectly fine when is up on a Docker Container. Thx!!
I solved the problem, it was because of my address ip on the connection string, idk why but changing it with local host made everything work!
mongo:
host: ${DB_URL:mongodb://localhost:27017}
username:
pass:
db: db
collection: musica
server.port: 8080
server:
error:
include-message: always
include-binding-errors: always
include-stacktrace: never
include-exception: false