1

I have developer Application in Spring boot and exported the war file and placed it in tomcat 9 server. When I try to test the API in Rest client the app is connecting to test DB instead of the DB which I declared in application.property for mongodb, below is my application.property

 Database name. spring.data.mongodb.database=IndianFarms Mongo server host. spring.data.mongodb.host=localhost Mongo server port. spring.data.mongodb.port=27017 logging logging.level.org.springframework.data=debug logging.level.=error spring.jackson.default-property-inclusion=NON_NULL 

Can any one help me in this.

7
  • 1
    And where is the configuration of test DB is mentioned in the application? Commented Jan 20, 2020 at 8:57
  • hi @suru , can u please share any exceptions that you are getting on start up? Commented Jan 20, 2020 at 9:27
  • @Smile i have not configured test DB. Commented Jan 20, 2020 at 9:58
  • Ok, so what exactly is the test DB? How do you know the application is connecting to a particular test DB? Commented Jan 20, 2020 at 10:00
  • 1
    Does this help stackoverflow.com/questions/36387265/…? Commented Jan 20, 2020 at 10:54

2 Answers 2

1

After lot of search and research in came to know that spring boot auto configure lot of things internally. Declaration in application.properties file is not working. We have to programmatically do this. Below is how I did it. Now Spring boot is connection to correct database which is in my case IndianFarms. @Smile thanks your suggestion helped me to find out exact cause.

@Configuration public class MongoConfig { @Bean public MongoDbFactory mongoDbFactory() { MongoClient mongoClient = new MongoClient("127.0.0.1:27017"); return new SimpleMongoDbFactory(mongoClient, "IndianFarms" ); } @Bean public MongoTemplate mongoTemplate() { return new MongoTemplate(mongoDbFactory()); } } 
Sign up to request clarification or add additional context in comments.

1 Comment

Gee it sure would be swell if you could add the imports namespaces for the type references...
0

I could make SpringBoot connect to my MongoDB running locally by setting the properties on application.properties.

You may have a look here https://docs.spring.io/spring-boot/reference/data/nosql.html#data.nosql.mongodb.connecting

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.