Basic CRUD with Spring Boot + MongoDB
This post describes how to build a basic CRUD SpringBoot application integrating a NoSQL database. I am using MongoDB Community Server for this project and MongoDBCompass.
Before proceeding download the MongoDB server from https://www.mongodb.com/try/download/community.
After downloading install it on your system. Make sure while installing you create the dir where mongo dB will store the data.
Note: If you are using Compass you don't need to run mongo.exe on cmd.
PreConditions:
- MongoDBCompass (as discussed above).
- I am using IntelliJ Idea ( other options are STS, Eclipse).
- PostMan https://www.postman.com/downloads/
It’s a Maven project. Let's get started with our basic CRUD application.
- Go to https://start.spring.io/
Select Maven as Project. I am using java 8 ( you can choose another version). Add two dependencies Spring Web and Spring Data MongoDB from adding dependencies. Click on Generate. We choose Jar so a jar file is downloaded. Extract the jar file.
2. Open your idea click on Open and browse your project. Select it and it will open in your idea. And it will look something like this.
Now we will create four packages and add respective files to them accordingly.
- Model Package: It will contain our model class. The member variables we define in our model class will form the columns of the table in a relational database and a non-relational database it will wrap up like documents.
- Repository Package: It will contain our Repo interface for MongoRepo
- Controller: This will contain the CRUD rest endpoints i.e. APIs.
- Service: It will contain the business logic layer classes for our rest endpoints.
Configuring application. properties file.
And Now we configure MongoDB configuration in our application. Go to src/main/resources. Open the application.properties file. Add the code below.
Here I created a database name “bookTable”.The starting URL is default URL which I didn’t configure mongodb://localhost:27017/<db-name>. Now our database is configured with our application.
Adding model class BookTableRequest.java
Let's write the model class and add some member variables to it.
Here the annotation @Document(collection = “tableRequest”) marks a class as being a domain object that we want to persist to our database with the collection name of our choice.
Setting up Repo for BookTableRequest
Spring provides interface MongoRepository<> which we have extended here.
Writing CRUD/Rest endpoints.
@PostMapping: It is used to save any record in our database.
@GetMapping: It fetches the records from our database.
@PutMapping: It is used to update our records. While using PutMapping we have to send the full object for updation. If we send only an updated member variable, the other member variables will be set to null in the database.
@DeleteMapping: When we want to delete any record.
@RestController annotation tells spring to create rest web service. It has mapped the requested date to the respected request handler method.
@RequestMapping() is used to globally provide the URL for all the end-points instead of writing it again in every request.
We have @Autowired the service class which is the business logic layer.
Adding Service class for rest endpoints.
We have autowired the repo to perform operations on our database.Spring provides various methods to perform operation on db like save( to save records) , delete(to delete the record) , findbyID( to find the record by id) which we used above in the code.
@Autowire annotation tells that a spring bean with this name somewhere in our project and spring mapped it for us.
Now application is ready to run.Open your postman and hit the endpoints.
Let’s add record using POST : localhost:8080/booking-portal/table-booking
See all the records using GET :localhost:8080/booking-portal/all-booking
Similarly hit other end-points.
Here we are done and we created simple bookingtable application.
Hope it will help you to understand how CRUD works.
Thank you ……….