Getting Started with Backend Development in Kotlin Using Spring Boot 3 & MongoDB
Rate this tutorial
Today, we are going to build a basic RESTful application that does a little more than a CRUD operation, and for that, we will use:
Spring Boot 3, which is one of the popular frameworks based on Spring, allowing developers to build production grades quickly.
Building a HelloWorld app in any programming language/technology, I believe, is the quickest and easiest way to get familiar with it. This helps you cover the basic concepts, like how to build, run, debug, deploy, etc.
Once you are on the website, you can update the default selected parameters for the project, like the name of the project, language, version of
Spring Boot, etc., to something similar as shown below.
And since we want to create REST API with MongoDB as a database, let's add the dependency using the Add Dependency button on the right.
After all the updates, our project settings will look like this.
Now we can download the project folder using the generate button and open it using the IDE. If we scan the project folder, we will only find one class — i.e.,
HelloBackendWorldApplication.kt, which has the
mainfunction, as well.
The next step is to print HelloWorld on the screen. Since we are building a restful application, we will create a
GETrequest API. So, let's add a function to act as a
We also need to add an annotation of
classto make it a
Now, let's run our project using the run icon from the toolbar.
It's time to understand the basics of
Spring Bootthat made it so easy to create our first API call.
This implies that it's a tool built on top of the Spring framework, allowing us to build web applications quickly.
Spring Bootuses annotations, which do the heavy lifting in the background. A few of them, we have used already, like:
@SpringBootApplication: This annotation is marked at class level, and declares to the code reader (developer) and Spring that it's a Spring Boot project. It allows an enabling feature, which can also be done using
@RestController: This annotation provides the routing information. Routing is nothing but a mapping of a
HTTPrequest path (text after
host/) to classes that have the implementation of these across various
These annotations are sufficient for building a basic application. Using Spring Boot, we will create a RESTful web service with all business logic, but we don't have a data container that can store or provide data to run these operations.
For our app, we will be using MongoDB as the database. MongoDB is an open-source, cross-platform, and distributed document database, which allows building apps with flexible schema. This is great as we can focus on building the app rather than defining the schema.
With the basics of MongoDB covered, now let's connect our Spring Boot project to it. Connecting with MongoDB is really simple, thanks to the Spring Data MongoDB plugin.
The format for the connection string is:
With all the basics covered, now let's build a more complex application than HelloWorld! In this app, we will be covering all CRUD operations and tweaking them along the way to make it a more realistic app. So, let's create a new project similar to the HelloWorld app we created earlier. And for this app, we will use one of the sample datasets provided by MongoDB — one of my favourite features that enables quick learning.
We will be using the
sample_restaurantscollection for our CRUD application. Before we start with the actual CRUD operation, let's create the restaurant model class equivalent to it in the collection.
You will notice there is nothing fancy about this class except for the annotation. These annotations help us to connect or co-relate classes with databases like:
@Document: This declares that this data class represents a document in Atlas.
@Field: This is used to define an alias name for a property in the document, like
coordfor coordinate in
Now let's create a repository class where we can define all methods through which we can access data.
Spring Boothas interface
MongoRepository, which helps us with this.
After that, we create a controller through which we can call these queries. Since this is a bigger project, unlike the HelloWorld app, we will create a separate controller where the
MongoRepositoryinstance is passed using
@Autowired, which provides annotations-driven dependency injection.
Now our project is ready to do some action, so let's count the number of restaurants in the collection using
Taking a step further to read the restaurant-based
restaurantId. We will have to add a method in our repo as
restaurantIdis not marked
@Idin the restaurant class.
Let's also validate this against a non-existing
As expected, we haven't gotten any results, but the API response code is still 200, which is incorrect! So, let's fix this.
In order to have the correct response code, we will have to check the result before sending it back with the correct response code.
To add a new object to the collection, we can add a
writefunction in the
repowe created earlier, or we can use the inbuilt method
MongoRepository. Since we will be adding a new object to the collection, we'll be using
Spring doesn't have any specific in-built update similar to other CRUD operations, so we will be using the read and write operation in combination to perform the update function.
Deleting a restaurant is also similar. We can use the
MongoRepositorydelete function of the item from the collection, which takes the item as input.