Knoldus Blog Audio
Spring Boot with MongoDB
Topics to cover in Spring Boot and MongoDB
In this blog we are going to see the beginning basics of what Spring Boot is? Ways to create Spring Boot Application? What is MongoDB? How to install MongoDB in Ubuntu? How to connect Spring Boot with MongoDB in Ubuntu? Let’s dive right in…
What is Spring Boot?
Spring Boot is a module of spring using which we speed up the development process. Spring Boot is not a new thing, it is a module of spring framework. Think of spring framework as a big house and Spring Boot as one of its room. So what makes development fast in Spring Boot? When we create a Spring Boot application, about 80% of the configuration is done by Spring Boot automatically. The rest 20% can be done as per the requirement of the user. As a result, the software development process becomes fast. A proper definition of Spring Boot is “Spring Boot makes it easy to create stand-alone, production-grade spring based applications that you can just run.”
It provides an easier and faster way to set up, configure and run both simple and web-based applications. Let’s see an equation to get more clarity about Spring Boot:
SPRING FRAMEWORK + EMBEDDED SERVERS(tomcat, etc) – [CONFIGURATIONS (xml)] = BOOT
Spring Boot provides convention over configuration software design style. In return it decreases the effort of the developer, it gives many things as default which we can change later if needed. One of the biggest advantage of Spring Boot is it scans the class path and find the dependency and automatically configure the things.
For example: We want to use MongoDB with Spring Boot, so we will simply give the dependency of MongoDB in pom.xml and it will configure it itself. We do not have to provide any additional configurations and just start using it.
Ways to create Spring Boot Application
Way 1 – Using spring Initializr
- Go to start.spring.io
2. Select your project type, Spring Boot version, enter the project meta data like Group, Artifact, Name, Description, Package Name, Packaging type and finally java version.
3. Now add the additional dependencies like Spring web, MongoDB (in case your are using) or as per your requirements by clicking on the Add Dependency tab and searching it in the dependency portion, shown as below.
4. Finally click on GENERATE. A zip folder of your spring application will be created. You can simply extract it and import it in your favourite IDE.
This is how the imported Spring Boot project will look like in IntelliJ:
Way 2 – Using IDEs
- Open IntelliJ. Go to File -> New ->Project
- In the left Pane of the new project select “Spring Assistant“.
3. In case you don’t see the option, go to File -> Settings -> Plugins and search for Spring Assistant plugin and install it Restart your IDE.
4. After restarting, do the 1st and 2nd step and press Next button.
5. Add all the project properties and press Next button.
6. Now add dependencies as per your requirement and press Next button.
7. Give a name to your Spring Boot Project and finally press Finish button. You are all set.
Way 3 – Creating a Spring Boot Project using STS
- Open the Spring Tool Suite.
- Click on the File menu -> New -> Maven Project. It will show a new Maven Project Wizard.
- Click on Next button.
- Select the maven-archetype-quickstart and click on the Next button.
- Give the Group Id and Artifact Id and finally click on Finish button.
Now we are done with creating the Spring Boot Application. Let’s know a little about MongoDB and other essentials to connect Spring Boot and MongoDB.
What is MongoDB?
We are all familiar with the traditional relational database which is SQL. But when we talk about MongoDB, it is a non-relational, open-source and document-oriented database management system and works on document-based database. MongoDB stores the data in JSON like documents which makes it more clear and understandable. Instead of using tables and rows, MongoDB uses collections and documents. These documents contain key-value pairs which is the basic unit of data in MongoDB. Collections actually contains set of documents and functions which is equivalent to relational database tables. MongoDB is used to store high volume of data and also because it has various features like Indexing, gives high performance etc.
This is how data in MongoDB looks like:
How to install MongoDB in Ubuntu (20.04 Focal)?
- Always update and upgrade you APT.
$ sudo apt update $ sudo apt upgrade
2. Now download and install MongoDB using the following terminal command.
$ sudo apt install mongodb
3. After installing, check the status of MongoDB using the below command.
$ sudo systemctl status mongodb
The status should be active (running)
4. Now start the MongoDB using the following command.
$ sudo systemctl start mongodb
5. To start the MongoDB Server use the following command.
$ sudo mongod
6. To start the MongoDB shell use the following command.
$ sudo mongo
7. To stop MongoDB use the below command.
$ sudo systemctl stop mongodb
8. In case you run into some errors use the below command to restart MongoDB.
$ sudo systemctl restart mongodb
Now you are done with the installation part. Let’s see how to connect Spring Boot and MongoDB.
Connection of MongoDB with Spring Boot Application
- While creating the Spring Boot Application, choose the MongoDB dependency. (If you are using the IDE way of creating the Spring Boot Application).
If you are using Spring Initializr, then search and add dependency from here:
MongoDB dependency in pom.xml will look like this:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency>
Next give the configuration properties in the properties file which is under Resources.
spring.data.mongodb.authentication-database=admin spring.data.mongodb.username=<username> spring.data.mongodb.password=<password> spring.data.mongodb.database=<databasename> spring.data.mongodb.port=27017 spring.data.mongodb.host=localhost
You can also use application.yml for giving MongoDB configurations.
Just right click on application.properties file and rename it as application.yml. But the way of giving configuration is different here, check below:
spring: data: mongodb: uri: mongodb://localhost:27017/databasename
Before running your Spring Boot Application, you will have to run your MongoDB server.
Now run your Spring Boot Application.
Small Application Example:
Created a Spring Boot Application using above steps named as DemoApplication.java.
Made a java class called Hello.java and added a basic String method there.
Now simply right click on the DemoApplication class, and run it.
Now go to browser and write, localhost:8080/user
This is what you will see:
Read Spring Boot documentation for more knowledge.
To gain more information visit Knoldus Blogs.