Reputation: 105
I'm trying to configure my Spring Boot backend to connect to a MongoDB hosted on MongoLab.
The problem is it tries to connect to localhost. The error I get is:
2017-06-12 17:34:05.046 INFO 11843 --- [127.0.0.1:27017]
org.mongodb.driver.cluster : Exception in monitor thread while connecting to server 127.0.0.1:27017
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:745) [na:1.8.0_25]
Caused by: java.net.ConnectException: Connection refused
at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.8.0_25]
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:345) ~[na:1.8.0_25]
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_25]
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_25]
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_25]
at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_25]
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
I tried excluding MongoAutoConfiguration.class but I get the same error.
My code:
FoodappBackendApplication.java:
@SpringBootApplication
@EnableAutoConfiguration(exclude={MongoAutoConfiguration.class})
//@SpringBootApplication(exclude = {MongoAutoConfiguration.class, MongoDataAutoConfiguration.class})
public class FoodappBackendApplication {
public static void main(String[] args) {
SpringApplication.run(FoodappBackendApplication.class, args);
}
}
application.properties:
server.port=8181
spring.data.mongodb.host=mongoserver
spring.data.mongodb.database=foodapp
spring.data.mongodb.uri=mongodb://<myusername>:<mypassword>@ds123456.mlab.com:12345/foodapp
spring.data.mongodb.authentication-database: admin
SpringMongoConfig.java:
@Configuration
public class SpringMongoConfig extends AbstractMongoConfiguration {
@Override
public String getDatabaseName() {
return "foodapp";
}
@Override
@Bean
public Mongo mongo() throws Exception {
return new MongoClient();
}
}
pom.xml dependencies:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver</artifactId>
<version>3.4.2</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>1.10.4.RELEASE</version>
</dependency>
</dependencies>
Any help is very much appreciated!
Upvotes: 1
Views: 1312
Reputation: 75934
Remove the @Bean mongo()
from SpringMongoConfig
and for that matter remove the class all together as you have already provided all the necessary configuration in the application.properties
file.
Update your FoodappBackendApplication
to
@SpringBootApplication
public class FoodappBackendApplication {
public static void main(String[] args) {
SpringApplication.run(FoodappBackendApplication.class, args);
}
}
For Java based configuration you can take a look at this.
Spring Boot does not read MongoDB configuration from Java class
Upvotes: 2
Reputation: 13116
Couple of issues I can see:
spring:
data:
mongodb:
uri: # note the indentation level of this key
which Spring then converts to a property spring.data.mongodb.uri
Upvotes: 1